2012-03-12 190 views
0

我寫了一個廣播接收器,每當互聯網開啓和關閉時都會接收廣播意圖。我想在我的服務中有一段代碼在互聯網可用時執行,在那個代碼中,我試圖調用一個不同的活動,但這導致了Logcat中的NullPointer異常。 plz幫助從公共服務方法調用活動時的NullPointerException

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的跟蹤:

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 

回答

0

嘗試:

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

它在Intent中提供Null指針異常callIntent = new Intent(getBaseContext(),MyActivity.class);和obj.update(); – 2012-03-14 19:59:57

0

嘗試這樣的:

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

它在Intent中提供Null指針異常callIntent = new Intent(getBaseContext(),MyActivity.class);和obj.update(); – 2012-03-14 19:56:11

0

在廣播接收器:如果你需要啓動服務,你寧願做這種方式:

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

你也需要指定在AndroidManifest.xml中的服務:

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

在服務中:調用活動directl是不好的做法y從服務。您應該通知系統狀態欄。如果用戶希望他可以點擊它來帶來活動。有關更多信息,請參見http://developer.android.com/guide/topics/ui/notifiers/notifications.html

相關問題