0

我正在开发一个简单的 Android 应用程序并收到“应用程序停止工作”错误。

当我退出我的preOnExecute方法时似乎会发生错误,但是如果我注释掉该应用程序无论如何都会落在下一个代码段上。

测试:打开屏幕/活动,有 5 个预填充的文本框和一个按钮。如果我单击该按钮,代码会命中一个在“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)
4

1 回答 1

2

错误是由第 109 行引起的:

String Title = editTextTitle.getText().toString();

这是一个 NullPointerException。最有可能的原因是类成员变量editTextTitle未在任何地方初始化。您通常会在您的 中初始化那些onCreate(),一段时间后,通过使用相关控件 IDsetContentView()调用。findViewById()

也就是说,“这里有一大段代码,请帮我调试”类型的问题在 StackOverflow 上是不受欢迎的。您可以通过在 AsyncTask 方法中放置一个断点并单步执行每个方法来轻松发现错误。那,或者仔细阅读 LogCat 中的异常跟踪。

编辑:LogCat 中的以下几行是相关的:

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)

行号就在那里。异常被捕获并重新抛出了几次 - 这是典型的。但要找到最终原因,您需要在堆栈跟踪中查找您的代码。

于 2012-09-20T16:30:10.657 回答