Hello everybody again,
I am developing one application that will create one txt file when it
runs first time and saves IMSI number in that file , After that when
phone boots every time it checks that IMSI number with IMSI number
which is stored at txt file. But when I run this program it gives me
Null pointer Exception. I have following code.
//FinalSMS.java
package com.micro;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;
import java.io.*;
public class FinalSMS extends Activity
{
/** Called when the activity is first created. */
File myFile;
String imsi;
String data;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createFile();
checkIMSI();
}
public void createFile()
{
try
{
myFile = new File ("tempt.txt");
if (myFile.exists())
{
FileReader fr = new FileReader(myFile);
BufferedReader br = new BufferedReader(fr);
data = br.readLine();
}
else
{
displayIMSI();
myFile.createNewFile();
PrintWriter pw = new PrintWriter(myFile);
TelephonyManager mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTelephonyMgr.getSubscriberId();
pw.println(imsi);
pw.flush();
pw.close();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void checkIMSI()
{
TelephonyManager mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTelephonyMgr.getSubscriberId();
if(data.equals(imsi))
{
sendSMS("9960510915","Sim has not changed.");
}
else
{
sendSMS("9960510915","Sim has been changed.");
}
}
public void displayIMSI()
{
TelephonyManager mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
imsi = mTelephonyMgr.getSubscriberId();
}
public void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,new
Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
0,new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic
failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No
service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver()
{
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS
delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not
delivered",Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI,
deliveredPI);
}
}
###############################################################
//MyStartupIntentReceiver .java
package com.micro;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
public class MyStartupIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/* Create intent which will finally start the Main-Activity.
*/
Intent myStarterIntent = new Intent(context,
FinalSMS.class);
/* Set the Launch-Flag to the Intent. */
//myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
myStarterIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/* Send the Intent to the OS. */
context.startActivity(myStarterIntent);
}
}
################################################################################
//AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.micro"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/
app_name">
<activity android:name=".FinalSMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /
>
</manifest>
#################################################################################
But when I run it it gives me following error messages
01-29 14:58:28.948: ERROR/AndroidRuntime(837): Uncaught handler:
thread main exiting due to uncaught exception
01-29 14:58:28.978: ERROR/AndroidRuntime(837):
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.micro/com.micro.FinalSMS}: java.lang.NullPointerException
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2268)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2284)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.ActivityThread.access$1800(ActivityThread.java:112)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.os.Handler.dispatchMessage(Handler.java:99)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.os.Looper.loop(Looper.java:123)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.ActivityThread.main(ActivityThread.java:3948)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
java.lang.reflect.Method.invokeNative(Native Method)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
java.lang.reflect.Method.invoke(Method.java:521)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:782)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
dalvik.system.NativeStart.main(Native Method)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): Caused by:
java.lang.NullPointerException
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
com.micro.FinalSMS.checkIMSI(FinalSMS.java:68)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
com.micro.FinalSMS.onCreate(FinalSMS.java:29)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1123)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2231)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): ... 11 more
######################################################
when I had checked whether the file is created or not in data/data/com/
micro/files/ The file is not present.
Because file is not created therefore it throws run time exception
(NullPointerException) , Please help me
Thanks
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en