从公共服务方法调用活动时出现NullPointerException

时间:2012-03-12 14:03:49

标签: android android-intent broadcastreceiver android-activity

我写了一个广播接收器,它可以在互联网开启和关闭时接收广播意图。我希望在我的服务中有一段代码可以在互联网可用时执行,在该代码中我试图调用不同的活动,但这导致Logcat中的NullPointer异常。 Plz帮助

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

3 个答案:

答案 0 :(得分:0)

尝试:

.....
    Intent callIntent= new Intent(getBaseContext(), MyActivity.class);
    callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.setAction(Intent.ACTION_CALL);
    getApplication().startActivity(callIntent);
.....

答案 1 :(得分:0)

试试这样:

Intent callIntent = new Intent(getBaseContext(), MyActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(callIntent);

答案 2 :(得分:0)

在广播接收器中:如果你需要启动服务,你宁愿这样做:

Intent myService = new Intent (context, MyService.class);
context.startService (myService);

您还需要在AndroidManifest.xml中指定您的服务:

<application ...>
    ...
    <service android:name=".MyService"></service>
    ...
</application>

在服务中:直接从服务调用活动是不好的做法。您应该将通知发送到系统状态栏。如果用户希望他可以点击它来进行活动。有关详细信息,请参阅http://developer.android.com/guide/topics/ui/notifiers/notifications.html