Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有Intent/IntentService的Android空指针异常_Java_Android - Fatal编程技术网

Java 带有Intent/IntentService的Android空指针异常

Java 带有Intent/IntentService的Android空指针异常,java,android,Java,Android,我的应用程序因返回以下堆栈跟踪而崩溃: 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

我的应用程序因返回以下堆栈跟踪而崩溃:

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)
mServiceIntent不会再次引用,直到下列行也位于MainActivity中:

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

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

如果你需要更多的信息,我很乐意添加。提前感谢您的耐心和帮助。

您应该在onStop()方法(在运行时发生)中实例化意图,而不是在编译时定义它。例如:

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");

使用此代码,希望它能正常工作

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

如何在
super
setContenView
之前使用
Intent
。您正在另一个函数中使用
mServiceIntent
,但尚未声明变量global。显示真正符合逻辑的代码。@Gaurav可能就是这个错误。这很有效,谢谢,我能够在多个方法中使用mServiceIntent而不会崩溃。
Intent intent = new Intent(this, SplashScreen.class);                          
 intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent );