2015-06-24 41 views
-3

有一個列表視圖,我使用AsyncTask從數據庫進行更新。然後當我點擊列表項目時,出現一個對話框,其中有兩個按鈕(一個是取消)。 當我點擊另一個按鈕時,我在數據庫的另一個表中插入一行。但是當我點擊按鈕時,應用程序崩潰。日誌文件是這樣的。php文件工作正常,列表視圖更新很好。同一活動中的多個AsyncTask

06-24 16:05:54.945: E/AndroidRuntime(15096): FATAL EXCEPTION: AsyncTask #4 
06-24 16:05:54.945: E/AndroidRuntime(15096): Process: com.bloodbank.slidingmenu, PID: 15096 
06-24 16:05:54.945: E/AndroidRuntime(15096): java.lang.RuntimeException: An error occured while executing doInBackground() 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.AsyncTask$3.done(AsyncTask.java:300) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.lang.Thread.run(Thread.java:818) 
06-24 16:05:54.945: E/AndroidRuntime(15096): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.Handler.<init>(Handler.java:200) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.Handler.<init>(Handler.java:114) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.widget.Toast$TN.<init>(Toast.java:336) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.widget.Toast.<init>(Toast.java:100) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.widget.Toast.makeText(Toast.java:250) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at com.bloodbank.slidingmenu.NeedBloodFragment$addBloodRequest.doInBackground(NeedBloodFragment.java:261) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at com.bloodbank.slidingmenu.NeedBloodFragment$addBloodRequest.doInBackground(NeedBloodFragment.java:1) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
06-24 16:05:54.945: E/AndroidRuntime(15096): ... 4 more 

代碼:

public class NeedBloodFragment extends Fragment { 

    private ProgressDialog pDialog; 
    ListView listView1; 

    JSONParser jParser = new JSONParser(); 

    JSONArray donors = null; 

    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_DONORS = "donors"; 

    List<NeedBloodItem> donorslist = new ArrayList<NeedBloodItem>(); 


    public NeedBloodFragment(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_need_blood, container, false); 

     new LoadAlldonors().execute(); 

     listView1 = (ListView)rootView.findViewById(R.id.listViewNeed); 


     listView1.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
       showCustomDialog(donorslist,position,NeedBloodFragment.this.getActivity().getApplicationContext()); 
      } 
     });   


     return rootView; 
    } 


    class LoadAlldonors extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      } 
    protected String doInBackground(String... args) { 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      params.add(new BasicNameValuePair("need", "A+")); 
      params.add(new BasicNameValuePair("bgroup", "A+")); 
      params.add(new BasicNameValuePair("candonate","yes")); 


      // getting JSON string from URL 
      Log.d("ip: ", AppConfig.URL_DONORS); 
      JSONObject json = jParser.makeHttpRequest(AppConfig.URL_DONORS, "POST", params); 


      try { 
       // Checking for SUCCESS TAG 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // donors found 
        // Getting Array of donors 
        donors = json.getJSONArray(TAG_DONORS); 

        // looping through All donors 
        for (int i = 0; i < donors.length(); i++) 
        { 
         JSONObject c = donors.getJSONObject(i); 

         // Storing each json item in variable 
         String name = c.getString("name"); 
         String email = c.getString("email"); 
         String contact = c.getString("phone"); 
         // creating new HashMap 
         donorslist.add(new NeedBloodItem(name,email,contact)); 

        } 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 

      // updating UI from Background Thread 
      getActivity().runOnUiThread(new Runnable(){ 
       public void run() { 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 
        ListAdapterNeedItem adapter = new ListAdapterNeedItem(getActivity(), 
          R.layout.need_list_row, donorslist); 
        listView1.setAdapter(adapter); 

       } 
      }); 

     } 

    } 
    protected void showCustomDialog(final List<NeedBloodItem> needlist,final int position,Context cnxt) { 
     // TODO Auto-generated method stub 
     final Dialog dialog = new Dialog(getActivity()); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.custom_dialog); 


     TextView tv1 = (TextView)dialog.findViewById(R.id.dialog_tv1); 
     TextView tv2 = (TextView)dialog.findViewById(R.id.dialog_tv2); 
     TextView tv3 = (TextView)dialog.findViewById(R.id.dialog_tv3); 

     tv1.setText(needlist.get(position).name); 
     tv2.setText(needlist.get(position).phone); 

     Button button1 = (Button)dialog.findViewById(R.id.dialog_btn1); 
     Button button2 = (Button)dialog.findViewById(R.id.dialog_btn2); 


     button1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       new addBloodRequest().execute(); 
       dialog.dismiss(); 
      } 
     }); 

     button2.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       dialog.dismiss(); 
      } 
     }); 

     dialog.show(); 
    } 

    class addBloodRequest extends AsyncTask<String, String, String> { 

     JSONParser jsonParser = new JSONParser(); 
     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 


      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("request", "re")); 
      params.add(new BasicNameValuePair("donorUID", "12")); 
      params.add(new BasicNameValuePair("reqUID", "1")); 
      params.add(new BasicNameValuePair("bloodgroup", "a+")); 
      params.add(new BasicNameValuePair("reqNAME", "fdee")); 
      params.add(new BasicNameValuePair("stateName", "dsds")); 
      params.add(new BasicNameValuePair("city", "svver")); 
      params.add(new BasicNameValuePair("needDate", "1961-01-01")); 
      params.add(new BasicNameValuePair("reqPhone", "8888")); 
      params.add(new BasicNameValuePair("donPhone", "1222")); 


      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(AppConfig.URL_REQUEST, 
        "POST", params); 

      // check log cat fro response 
      Log.d("Create Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        Toast.makeText(getActivity().getApplicationContext(), 
          "Request Added to Database", Toast.LENGTH_LONG) 
          .show(); 
        Log.d("Success","Request Added to Database"); 

       } else { 
        Toast.makeText(getActivity().getApplicationContext(), 
          "Request Can't be Added to Database", Toast.LENGTH_LONG) 
          .show(); 
        Log.e("Error","Request could not be added to Database"); 
        // failed to create product 
       } 
      } 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 done 

     } 

    } 

} 

編輯:我已經包含代碼的有關問題的部件。

+0

我覺得你是一個'Toast.maketText ()'在'doInBackGround()'方法中。你不能在後臺線程中進行UI操作。發佈您的代碼。 – Emil

+0

看看這個:http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare – Rich

+0

張貼您的代碼 – deniz

回答

0

使用runOnUiThreaddoInBackground顯示敬酒

getActivity().runOnUiThread(new Runnable() {     
    @Override 
    public void run() {       
     Toast.makeText(Activity.this, "Message", Toast.LENGTH_SHORT).show(); 
    } 
}); 
+0

。謝謝。 – divya

+0

只是一個編輯:它是一個片段。所以它應該是getActivity()。runOnUiThread(new Runnable()) – divya

+0

是的酷編輯我的帖子太。快樂你工作。將其標記爲已接受,以便其他用戶可以輕鬆地輕鬆查看解決方案。 –