Я написал приемник вещания, который получает намерение вещания всякий раз, когда Интернет включен и выключен. Я хочу, чтобы часть кода в моем сервисе выполнялась всякий раз, когда Интернет доступен, в этом коде я пытаюсь вызвать другое действие, но это приводит к исключению NullPointer в Logcat.
Пожалуйста, помогите
NetworkReceiver:
public class NetworkReceiver extends BroadcastReceiver { //
public static final String TAG = "NetworkReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
String typeName = info.getTypeName();
String subtypeName = info.getSubtypeName();
boolean available = info.isAvailable();
Log.e(TAG, "Network Type: " + typeName
+ ", subtype: " + subtypeName
+ ", available: " + available);
if(available)
{
MyService obj=new MyService();
obj.update();
//Log.i(TAG,"SERVICE CALLED");
}
else
Log.i(TAG,"NET UNAVAILABLE");
}
}
}
MyService:
public class MyService extends Service {
public void onCreate() {
//some code
}
public int onStartCommand(Intent intent, int flags, int startId) {
//some code
}
public void update(){
//some code
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.i("TAG","before PACKAGE NAME SET");
callIntent.setClass(getBaseContext(),MyActivity.class); //Line 50
startActivity(callIntent);
}
}
Logcat Trace:
03-12 19:06:00.925: ERROR/AndroidRuntime(509): Uncaught handler: thread main exiting due to uncaught exception
03-12 19:06:01.035: ERROR/AndroidRuntime(509): java.lang.RuntimeException: Unable to start receiver com.gooogle.omcsa.NetworkReceiver: java.lang.NullPointerException
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.app.ActivityThread.access$3100(ActivityThread.java:119)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.os.Looper.loop(Looper.java:123)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at java.lang.reflect.Method.invoke(Method.java:521)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at dalvik.system.NativeStart.main(Native Method)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): Caused by: java.lang.NullPointerException
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.content.ComponentName.<init>(ComponentName.java:75)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.content.Intent.setClass(Intent.java:4688)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at com.gooogle.omcsa.MyService.update(MyService.java:50)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at com.gooogle.omcsa.NetworkReceiver.onReceive(NetworkReceiver.java:39)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)
03-12 19:06:01.035: ERROR/AndroidRuntime(509): ... 10 more
03-12 19:06:01.125: ERROR/dalvikvm(509): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-12 19:06:02.865: ERROR/ActivityThread(52): Failed to find provider info for android.server.checkin