dongzong7467 2012-09-20 16:11
浏览 61
已采纳

Android应用:页面停止工作

I am developing a simple Android app and am getting a 'App stopped working' error.

The error seems to happen when I exit my preOnExecute method but if I comment that out the app falls over on next code piece anyway.

Test: Open screen/activity, there are 5 pre filled textboxes and a button. If I click the button the code hits a class that opens a dialog in 'OnPreExecute' and it fails on execiting this method.

Its probably something to do with the background thread not liking whats happening on UI but i dont know.

Any idea how I can troubleshoot this?

See below for code

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();
           }
       }
}

I get the following 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)
  • 写回答

1条回答

  • douxing9813 2012-09-20 16:30
    关注

    The error is caused by line 109:

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

    And it's a NullPointerException. Most likely, it's caused by the fact that class member variable editTextTitle is not initialized anywhere. You typically initialize those in your onCreate(), some time after setContentView(), by calling findViewById() with the relevant control ID.

    That said, questions of the type "here's a large chunk of code, please debug for me" are frowned upon here at StackOverflow. You could've easily caught the error by placing a breakpoint inside the AsyncTask methods and stepping through each of them. That, or carefully reading the exception trace in the LogCat.

    EDIT: the following lines in the LogCat are relevant:

    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)
    

    The line number is right there. The exception was caught and rethrown a few times - that's typical. But to find the ultimate cause, you look for your code in the stack trace.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)