简体   繁体   English

具有Intent / IntentService的Android空指针异常

[英]Android Null Pointer Exception with Intent/IntentService

My application crashes returning the following stack trace: 我的应用程序崩溃,返回以下堆栈跟踪:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.**REDACTED**.copyright/com.example.**REDACTED**.copyright.MainActivity}: java.lang.NullPointerException
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.os.Looper.loop(Looper.java:136)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.reflect.Method.invoke(Method.java:515)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at dalvik.system.NativeStart.main(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933): Caused by: java.lang.NullPointerException
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.content.ComponentName.<init>(ComponentName.java:77)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.content.Intent.<init>(Intent.java:3834)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at com.example.**REDACTED**.copyright.MainActivity.<init>(MainActivity.java:31)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.Class.newInstanceImpl(Native Method)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at java.lang.Class.newInstance(Class.java:1208)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-21 22:26:18.499 E/AndroidRuntime(15933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2119)

From what I can tell the error occurs in line 31 of my MainActivity class line 31 and a few of the surrounding lines are shown below: 据我所知,该错误发生在MainActivity类的第31行的第31行中,下面显示了一些周围的行:

public class MainActivity extends Activity {
Intent mServiceIntent = new Intent(this, Scan.class);

mServiceIntent is not reference again until the the lines shown below which are also in MainActivity: 直到下面显示的行也位于MainActivity中,才再次引用mServiceIntent:

 @Override
protected void onStop() {
    super.onStop();

    this.startService(mServiceIntent);
    Log.v("MainActivity", "onStop called");

If there is anymore information you need I will be happy to add it. 如果您还有其他信息,我们将很乐意添加。 Thank you in advance for your patience and assistance. 预先感谢您的耐心配合和帮助。

You should instantiate the intent in your onStop() method (which happens during runtime) as opposed to defining it in compile time. 您应该在onStop()方法中实例化意图(在运行时发生),而不是在编译时进行定义。 Eg: 例如:

public class MainActivity extends Activity {
    Intent mServiceIntent;
...
@Override
protected void onStop() {
    super.onStop();
    mServiceIntent = new Intent(this, Scan.class);
    this.startService(mServiceIntent);
    Log.v("MainActivity", "onStop called");

Use this code hope this will worked well 使用此代码希望它能很好地工作

Intent intent = new Intent(this, SplashScreen.class);                          
 intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM