2012-09-20 41 views
0

我正在開發一個簡單的Android應用程序,並且正在收到'應用程序停止工作'錯誤。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 [email protected] 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 [email protected] 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) 
+0

你能告訴我們crashtraptrace嗎? – Stericson

+0

日誌貓確定還是你想要其他東西? – user1417337

+0

你有一個nullpointerexception在你的doInBackground(String ... args)方法中,只需運行一個調試 –

回答

2

錯誤是由線109引起的:

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

它是一個NullPointerException異常。很可能,這是由於類成員變量editTextTitle未在任何地方初始化的事實造成的。您通常通過findViewById()與相關的控件ID來初始化onCreate()中的某些時間,setContentView()之後的某個時間。

這就是說,類型的問題,「這裏的代碼一大塊,請調試我。」在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) 

的行號就在那裏。這個例外被捕獲並重新拋出了幾次 - 這是典型的。但爲了找到最終原因,您需要在堆棧跟蹤中查找代碼。

+0

嗨,謝謝,我無法看到它說空指針exeption和代碼似乎錯誤無論我做了什麼與先前我認爲編號整個事情。什麼告訴你它在109行的空指針? – user1417337

+0

您的堆棧跟蹤顯示信息: 09-20 16:20:39.463:E/AndroidRuntime(658):導致:java.lang.NullPointerException 09-20 16:20:39.463:E/AndroidRuntime(658)在android22.voter.namespace.AppCreateQuestionActivity $ SaveQuestionDetails.doInBackground(VoterCreateQuestionActivity.java:109) – Stericson

+0

感謝密爾塞瓦。當我醒來時,我必須這樣做! – user1417337