我一直在努力找出这个问题很久了,希望你们能有所帮助。我正在通过单击按钮运行IntentService,并且出现了空指针异常。这是我的代码:

主要活动:

public class MainActivity extends Activity {

    private ResponseReceiver receiver;
    private Button getButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        receiver = new ResponseReceiver();
        registerReceiver(receiver, filter);

        getButton = (Button) findViewById(R.id.button1);
        setGetButtonOnClickListener();
    }

    public void setGetButtonOnClickListener()
    {
        getButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View  v)
            {
                Intent msgIntent = new Intent(getApplicationContext(), XMLIntentService.class);
                msgIntent.putExtra(XMLIntentService.PARAM_IN_MSG, "test");
                startService(msgIntent);
                Toast.makeText(getApplicationContext(), "intent started" , Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ResponseReceiver extends BroadcastReceiver {
       public static final String ACTION_RESP = "com.example.intent.action.MESSAGE_PROCESSED";
       @Override
        public void onReceive(Context context, Intent intent) {
           String information = intent.getStringExtra(XMLIntentService.PARAM_OUT_MSG);
           information += " yay!";
          Toast.makeText(getApplicationContext(), information , Toast.LENGTH_SHORT).show();
       }
    }
}


XMLIntentService

public class XMLIntentService extends IntentService {
    public static final String PARAM_IN_MSG = "omsg";
    public static final String PARAM_OUT_MSG = "omsg";

    public XMLIntentService() {
        super("XMLIntentService");
        Toast.makeText(getApplicationContext(), "Intent Service Called" , Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(getApplicationContext(), "On handle intent called" , Toast.LENGTH_SHORT).show();
        String msg = intent.getStringExtra(PARAM_IN_MSG);

        //Get XML info here
        String resultTxt = "Yayy";
        Log.v("XMLIntentService", "Handling msg: " + resultTxt);

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);
        sendBroadcast(broadcastIntent);
    }
}


日志文件:

11-08 23:06:31.130: E/Trace(1304): error opening trace file: No such file or directory (2)
11-08 23:06:31.902: D/gralloc_goldfish(1304): Emulator without GPU emulation detected.
11-08 23:06:39.922: D/AndroidRuntime(1304): Shutting down VM
11-08 23:06:39.922: W/dalvikvm(1304): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-08 23:06:39.951: E/AndroidRuntime(1304): FATAL EXCEPTION: main
11-08 23:06:39.951: E/AndroidRuntime(1304): java.lang.RuntimeException: Unable to instantiate service com.example.cis345lab5.XMLIntentService: java.lang.NullPointerException
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2347)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.app.ActivityThread.access$1600(ActivityThread.java:130)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.os.Looper.loop(Looper.java:137)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at java.lang.reflect.Method.invoke(Method.java:511)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at dalvik.system.NativeStart.main(Native Method)
11-08 23:06:39.951: E/AndroidRuntime(1304): Caused by: java.lang.NullPointerException
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at com.example.cis345lab5.XMLIntentService.<init>(XMLIntentService.java:16)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at java.lang.Class.newInstanceImpl(Native Method)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at java.lang.Class.newInstance(Class.java:1319)
11-08 23:06:39.951: E/AndroidRuntime(1304):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2344)
11-08 23:06:39.951: E/AndroidRuntime(1304):     ... 10 more


在我的Android Manifest.xml中

<service android:enabled="true"
            android:name=".XMLIntentService"></service>

最佳答案

问题是XmlIntentService构造函数中的这一行:

Toast.makeText(getApplicationContext(), "Intent Service Called" , Toast.LENGTH_SHORT).show();


构造IntentService时(特别是在调用attach(...)之前),父Service中的mBase成员变量为null。对getApplicationContext的调用试图在父服务类中取消对mBase的引用,从而导致NullReferenceException。

解决方案是从XmlIntentService构造函数中删除Toast.makeText行。

10-05 19:50