0

AsyncTask에 이상한 문제가 있습니다. AsyncTask를 사용하여 내 웹 사이트의 데이터를로드하고 이러한 데이터로 ListView를 만듭니다. 각 항목에 대한 청취자를 설정합니다. 항목을 클릭하면 새로운 활동이 시작됩니다.활동을 변경 한 후 AsyncTask가 다시로드됩니다.

하지만 새 활동이로드되면 이전 활동 (asynctask)이 다시로드되어 내 응용 프로그램이 중단됩니다.

(가 더 이상 존재하지 않는 목록보기 수정하려고하기 때문에) 내 AsyncTask를의 상태를 테스트하고 그것은

package com.wglxy.example.dash1; 

import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.DialogInterface.OnCancelListener; 
import android.os.AsyncTask; 
import android.os.Bundle; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.SimpleAdapter; 


public class F1Activity extends DashboardActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView (R.layout.activity_f2); 
    setTitleFromActivityLabel (R.id.title_text); 

    // Je démarre l'AsyncTask qui charge les interventions 
    getInterventionListing Listing = new getInterventionListing(); 
    Listing.execute();  
} 


private class getInterventionListing extends AsyncTask<Integer, Integer, ArrayList<HashMap<String, String>>> 
{ 
    private ProgressDialog dialog; 


    @Override 
    protected void onPreExecute() 
    { 
     // Dialog qui s'affiche durant l'AsyncTask 
     dialog = ProgressDialog.show(F1Activity.this, "Chargement", "Chargement des données",true,true,new OnCancelListener() { 
      public void onCancel(DialogInterface dialog) { 
       dialog.dismiss(); 
       cancel(true); 
      }}); 
    } 

    @Override 
    protected ArrayList<HashMap<String, String>> doInBackground(Integer... params) 
    { 
     InputStream is = null; 
     String result = ""; 
     String problemes = ""; 
     String prenom = ""; 
     String strURL = "http://monsiteweb.com/fichier.php"; 
     ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); 


     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("listing_intervention","")); 

     // Requête HTTP 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(strURL); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection " + e.toString()); 
     } 


     HashMap<String, String> map; 


     // Convertion de la requête en string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     // Parse des données JSON 
     try{ 
      JSONArray jArray = new JSONArray(result); 

      for(int i=0;i<jArray.length();i++){ 
       JSONObject json_data = jArray.getJSONObject(i); 
       if (json_data.getString("problemes") != "null") 
        problemes = json_data.getString("problemes"); 
       if (json_data.getString("prenom") != "null") 
        prenom = json_data.getString("prenom"); 

       map = new HashMap<String, String>(); 
       map.put("titre", "N°" + json_data.getInt("id_intervention") + " : " + json_data.getString("nom") + " " + prenom); 
       map.put("description", problemes); 
       map.put("id_intervention", json_data.getString("id_intervention")); 
       map.put("id_client", json_data.getString("id_client")); 
       if (json_data.getInt("statut") == 0) 
       { 
        map.put("img", String.valueOf(R.drawable.onwork)); 
       } 
       else if (json_data.getInt("statut") == 2) 
       { 
        map.put("img", String.valueOf(R.drawable.onwait)); 
       } 
       else if (json_data.getInt("statut") == 6) 
       { 
        map.put("img", String.valueOf(R.drawable.home)); 
       } 
       else 
       { 
        map.put("img", String.valueOf(R.drawable.other)); 
       } 

        listItem.add(map); 

      } 

     }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
     return listItem; 

    } 


    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> listItem) 
    { 
     ListView maListViewPerso = (ListView) findViewById(R.id.listviewperso); 
     SimpleAdapter mSchedule = new SimpleAdapter (F1Activity.this, listItem, R.layout.listview, 
       new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description}); 
     maListViewPerso.setAdapter(mSchedule); 


     maListViewPerso.setOnItemClickListener(new OnItemClickListener() {    
      public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
       ListView maListViewPerso = (ListView) findViewById(R.id.listviewperso); 
       HashMap<String, String> items_loaded = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position); 
       Intent defineIntent = new Intent(F1Activity.this, Details_intervention.class); 
       Bundle objetbundle = new Bundle(); 
       objetbundle .putString("id_intervention",items_loaded.get("id_intervention").toString()); 
       objetbundle .putString("id_client",items_loaded.get("id_client").toString()); 
       defineIntent.putExtras(objetbundle); 
       F1Activity.this.startActivity(defineIntent); 
      } 
     }); 

     dialog.dismiss(); 
    } 



} 

} 

(항상 실행하지만 onPostExecute()가 수행된다 반환) 완료 결코 -

Details_intervention.class -
03-03 16:58:27.300: W/dalvikvm(656): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
03-03 16:58:27.320: E/AndroidRuntime(656): FATAL EXCEPTION: main 
03-03 16:58:27.320: E/AndroidRuntime(656): java.lang.NullPointerException 
03-03 16:58:27.320: E/AndroidRuntime(656): at com.wglxy.example.dash1.F1Activity$getInterventionListing.onPostExecute(F1Activity.java:164) 
03-03 16:58:27.320: E/AndroidRuntime(656): at com.wglxy.example.dash1.F1Activity$getInterventionListing.onPostExecute(F1Activity.java:1) 
03-03 16:58:27.320: E/AndroidRuntime(656): at android.os.AsyncTask.finish(AsyncTask.java:602) 

답변

0

여전히 지원이 필요한 경우는 ...

당신은 당신이 onclick을 통해 호출하는 활동에 대한 코드를 게시 할 수 있습니까?

몇 노트 :

  1. 당신은 단지 startActivity(defineIntent)을함으로써 활동을 시작할 수 있습니다, 당신은

  2. F1Activity.this.이 까다 롭고되지 않을 필요가없는,하지만 당신은 전체에 약간의 자바 코딩 규칙을 깨고있다. 그 응용 프로그램을 해치지는 않겠지 만, 언뜻 보면 표준과 이상한 것들이 있습니다 : 예는 private class getInterventionListing입니다 - 클래스는 대문자로 시작해야하며 "get"및 "set"머리말은 메서드에만 사용됩니다. 따라서 private class DownloadInterventionListing이 더 좋을 것입니다.

+0

Details_intervention.class는 내 문제의 해결책 인 잘못된 활동을 확장했습니다. 팁 주셔서 감사합니다, 나는 것들을 바꿀 것입니다 :) –

+0

굉장! 다행이야.) – agentcurry

관련 문제