【发布时间】:2015-04-18 12:20:39
【问题描述】:
我正在尝试将一些数据从文件保存到数据库。不使用intentService 一切都很好。但是,当我使用 IntentService 时,我的应用程序崩溃了,并且我的 asyncTask 出现错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
Caused by: java.lang.NullPointerException
at com.example.carl.myTest.AsyncTask.doInBackground(AsyncTask.java:44)
第 44 行是:HttpResponse response = httpClient.execute(httpPost);
我的问题是为什么没有服务一切都很好,使用后又乱了?!!!出了什么问题? - 非常感谢任何帮助。我展示了我的 intentService 类:
public class TheIntentService extends IntentService {
MainActivityMyTest t = new MainActivityMyTest ();
private Handler handler = new Handler();
public MyIntentService() {
super("TheIntentService ");
}
@Override
protected void onHandleIntent(Intent intent) {
t.writeToTable();
handler.post(showResult);
}
private Runnable showResult = new Runnable() {
public void run() {
Context c = TheIntentService .this.getApplicationContext();
Toast.makeText(c, "Mission accomplished", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onDestroy() {
Context c = this.getApplicationContext();
Toast.makeText(c, "Exits service", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
然后从 MainActivity 我像这样启动服务:
Intent i = new Intent(MainActivityMyTest.this, TheIntentService.class);
startService(i);
【问题讨论】:
标签: android android-asynctask android-intentservice