シンプルな Android アプリを開発していますが、「アプリが動作を停止しました」というエラーが表示されます。
メソッドを終了するとエラーが発生するようですpreOnExecute
が、それをコメントアウトすると、とにかく次のコード部分でアプリがフォールオーバーします。
テスト: 画面/アクティビティを開くと、事前に入力された 5 つのテキストボックスと 1 つのボタンがあります。ボタンをクリックすると、「OnPreExecute」でダイアログを開くクラスにコードがヒットし、このメソッドの実行に失敗します。
おそらく、バックグラウンド スレッドが UI で起こっていることを好まないことに関係していますが、私にはわかりません。
これをトラブルシューティングする方法はありますか?
コードについては以下を参照してください
package android22.app.namespace;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android22.voter.namespace.appSubmitAnswerActivity.GetQuestionDetails;
import android22.voter.namespace.appSubmitAnswerActivity.SaveAnswerDetails;
public class appCreateQuestionActivity extends Activity {
//controls
EditText editTextTitle;
EditText editTextQuestionString;
EditText editTextA1;
EditText editTextA2;
EditText editTextA3;
EditText editTextA4;
EditText editTextA5;
Button buttonSubmitQuestion;
// Progress Dialog
private ProgressDialog qDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single question url
private static final String url_insert_question_details = "http://xxx.xxx.xxx.xxx/voter/Voter_Db_CreateNewQuestion.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_QUESTION = "question";
private static final String TAG_QID = "Qid";
private static final String TAG_TITLE = "Title";
private static final String TAG_QUESTIONSTRING = "QuestionString";
private static final String TAG_A1 = "A1";
private static final String TAG_A2 = "A2";
private static final String TAG_A3 = "A3";
private static final String TAG_A4 = "A4";
private static final String TAG_A5 = "A5";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.createquestion);
// save button
buttonSubmitQuestion = (Button) findViewById(R.id.btnCreate);
/*Set Control Listeners */
buttonSubmitQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// starting background task to update question
new SaveQuestionDetails().execute();
}
});
}
class SaveQuestionDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
qDialog = new ProgressDialog(VoterCreateQuestionActivity.this);
qDialog.setMessage("Saving ...");
qDialog.setIndeterminate(false);
qDialog.setCancelable(true);
qDialog.show();
}
/**
* Saving question
* */
protected String doInBackground(String... args) {
String Title = editTextTitle.getText().toString();
String Question = editTextQuestionString.getText().toString();
String A1 = editTextA1.getText().toString();
String A2 = editTextA2.getText().toString();
String A3 = editTextA3.getText().toString();
String A4 = editTextA4.getText().toString();
String A5 = editTextA5.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_QID, "1"));
params.add(new BasicNameValuePair(TAG_TITLE, Title));
params.add(new BasicNameValuePair(TAG_QUESTION, Question));
params.add(new BasicNameValuePair(TAG_A1, A1));
params.add(new BasicNameValuePair(TAG_A2, A2));
params.add(new BasicNameValuePair(TAG_A3, A3));
params.add(new BasicNameValuePair(TAG_A4, A4));
params.add(new BasicNameValuePair(TAG_A5, A5));
// sending modified data through http request
// Notice that update question url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_insert_question_details,
"POST", params);
// check json success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully updated
Intent i = getIntent();
// send result code 100 to notify about question update
setResult(100, i);
finish();
} else {
// failed to update question
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once question updated
qDialog.dismiss();
}
}
}
次のLogCatを取得します
09-20 16:03:15.703: D/gralloc_goldfish(658): Emulator without GPU emulation detected.
09-20 16:20:39.298: W/dalvikvm(658): threadid=12: thread exiting with uncaught exception (group=0x409961f8)
09-20 16:20:39.463: E/AndroidRuntime(658): FATAL EXCEPTION: AsyncTask #1
09-20 16:20:39.463: E/AndroidRuntime(658): java.lang.RuntimeException: An error occured while executing doInBackground()
09-20 16:20:39.463: E/AndroidRuntime(658): at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.lang.Thread.run(Thread.java:856)
09-20 16:20:39.463: E/AndroidRuntime(658): Caused by: java.lang.NullPointerException
09-20 16:20:39.463: E/AndroidRuntime(658): at android22.voter.namespace.AppCreateQuestionActivity$SaveQuestionDetails.doInBackground(VoterCreateQuestionActivity.java:109)
09-20 16:20:39.463: E/AndroidRuntime(658): at android22.voter.namespace.AppCreateQuestionActivity$SaveQuestionDetails.doInBackground(VoterCreateQuestionActivity.java:1)
09-20 16:20:39.463: E/AndroidRuntime(658): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-20 16:20:39.463: E/AndroidRuntime(658): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-20 16:20:39.463: E/AndroidRuntime(658): ... 4 more
09-20 16:20:41.493: E/WindowManager(658): Activity android22.voter.namespace.AppCreateQuestionActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416e3798 that was originally added here
09-20 16:20:41.493: E/WindowManager(658): android.view.WindowLeaked: Activity android22.voter.namespace.AppCreateQuestionActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416e3798 that was originally added here
09-20 16:20:41.493: E/WindowManager(658): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
09-20 16:20:41.493: E/WindowManager(658): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
09-20 16:20:41.493: E/WindowManager(658): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
09-20 16:20:41.493: E/WindowManager(658): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
09-20 16:20:41.493: E/WindowManager(658): at android.view.Window$LocalWindowManager.addView(Window.java:537)
09-20 16:20:41.493: E/WindowManager(658): at android.app.Dialog.show(Dialog.java:274)
09-20 16:20:41.493: E/WindowManager(658): at android22.voter.namespace.AppCreateQuestionActivity$SaveQuestionDetails.onPreExecute(VoterCreateQuestionActivity.java:101)
09-20 16:20:41.493: E/WindowManager(658): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
09-20 16:20:41.493: E/WindowManager(658): at android.os.AsyncTask.execute(AsyncTask.java:511)
09-20 16:20:41.493: E/WindowManager(658): at android22.voter.namespace.VoterCreateQuestionActivity$1.onClick(AppCreateQuestionActivity.java:81)
09-20 16:20:41.493: E/WindowManager(658): at android.view.View.performClick(View.java:3480)
09-20 16:20:41.493: E/WindowManager(658): at android.view.View$PerformClick.run(View.java:13983)
09-20 16:20:41.493: E/WindowManager(658): at android.os.Handler.handleCallback(Handler.java:605)
09-20 16:20:41.493: E/WindowManager(658): at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 16:20:41.493: E/WindowManager(658): at android.os.Looper.loop(Looper.java:137)
09-20 16:20:41.493: E/WindowManager(658): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-20 16:20:41.493: E/WindowManager(658): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 16:20:41.493: E/WindowManager(658): at java.lang.reflect.Method.invoke(Method.java:511)
09-20 16:20:41.493: E/WindowManager(658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 16:20:41.493: E/WindowManager(658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 16:20:41.493: E/WindowManager(658): at dalvik.system.NativeStart.main(Native Method)