当另一个应用程序执行时,在android上发生了NullPointerException

时间:2016-05-17 01:03:47

标签: java android nullpointerexception

在主应用程序中,当我执行另一个应用程序时,发生NullPointerException。

这个logcat在这里

java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.co.iosystem.blackeyeonandroid/kr.co.iosystem.blackeyeonandroid.BlackEyeActivity}: 
java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
android.app.ActivityThread.access$800(ActivityThread.java:135)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:136)
android.app.ActivityThread.main(ActivityThread.java:5001)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NullPointerException
android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
android.app.Activity.startActivityForResult(Activity.java:3424)
android.app.Activity.startActivityForResult(Activity.java:3385)
android.app.Activity.startActivity(Activity.java:3627)
android.app.Activity.startActivity(Activity.java:3595)
kr.co.iosystem.blackeyeonandroid.BlackEyeActivity.netCheckAutoUpdate(BlackEyeActivity.java:479)
kr.co.iosystem.blackeyeonandroid.BlackEyeActivity.onCreate(BlackEyeActivity.java:147)
android.app.Activity.performCreate(Activity.java:5231)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) E/AndroidRuntime( 2678):     ... 11 more

和我的代码。

protected static Context mainContext =null;

@Override   
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main);
netCheckAutoUpdate();   //NullPointerException 
}



public void netCheckAutoUpdate() {
ConnectivityManager connectivityManager = (ConnectivityManager)   this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ethernet = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
        Intent intent = mainContext.getPackageManager().getLaunchIntentForPackage("package name");
        startActivity(intent);    //NullPointerException

} else if (ethernet.isConnected()) {
        Intent intent = mainContext.getPackageManager().getLaunchIntentForPackage("package name");
        startActivity(intent);     //NullPointerException


} else {
        Intent intent = mainContext.getPackageManager().getLaunchIntentForPackage("package name");
        startActivity(intent);    //NullPointerException


}
}
或许,我必须使用空检查?

如果这样正确,怎么用null检查?   mainContext是全局变量

当我连接wifi时。发生NullPointerException   这部分

  NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

  if (wifi.isConnected()) {
        Intent intent =         mainContext.getPackageManager().getLaunchIntentForPackage("kr.co.iosystem.be_update");
        startActivity(intent);

  }

换句话说,

 `startActivity(intent)` here occur NullPointerException.

我是清单的适当许可。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

1 个答案:

答案 0 :(得分:0)

您的mainContext为空且无法获取包裹原因mainContext没有您的上下文。

尝试使用getContext() / this或使用片段getActivity()

了解更多信息背景:https://developer.android.com/reference/android/content/Context.html

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"