gpt4 book ai didi

android - 单击按钮时启动 IntentService 错误

转载 作者:行者123 更新时间:2023-11-29 21:55:55 24 4
gpt4 key购买 nike

长期以来我一直在努力解决这个问题,希望你们能提供帮助。我正在通过单击按钮运行 IntentService,但出现空指针异常。这是我的代码:

主要 Activity :

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 行。

关于android - 单击按钮时启动 IntentService 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com