2013-07-18 111 views
-2

嗨我是初學者的android,我不知道很習慣asychtask但之後,我意識到我繼續作爲應用程序被迫停住和錯誤日誌致命異常:AsyncTask#1 ,感謝您的幫助,我需要提取一個列表視圖ANDROID致命異常:AsyncTask#1

package com.example.test; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.util.ArrayList; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.graphics.Color; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Actu_Connextion extends Activity { 

    Actualite oActualite; 
    ArrayList<Actualite> listeActualites; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.acutalite_main); 
     int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); 
     TextView actionBarTextView = (TextView)findViewById(actionBarTitleId); 
     actionBarTextView.setTextColor(Color.WHITE); 
     this.listeActualites = new ArrayList<Actualite>(); 
     new DownloadTask().execute(); 

    } 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.refresh, menu); 
     return true; 
    } 
    public boolean onOptionsItemSelected(MenuItem item){  // Quand on appuis sur l/'item 
      switch (item.getItemId()){ 

       case R.id.refresh:      // selectionne l'item par id 
        return true; 

       case R.id.back_menu_principale:      // selectionne l'item par id 
        Intent intent = new Intent(getBaseContext() ,MainActivity.class); 
        startActivity(intent); 
       default: 
        return super.onOptionsItemSelected(item); 
      } 
     } 


    public class DownloadTask extends AsyncTask<String, Integer, ArrayList<Actualite>>{ 


     ArrayList<Actualite> listeActualites; 
      // Invoked by execute() method of this object 
      @Override 
      protected ArrayList<Actualite> doInBackground(String... jsonData) { 

     StringBuffer stringB = new StringBuffer(""); 
     BufferedReader bufR = null; 

     try{ 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet requete = new HttpGet(); 
      URI uri = new URI("http://192.168.1.3/actu.php"); 
      requete.setURI(uri); 
      HttpResponse reponse = client.execute(requete); 
      InputStream is = reponse.getEntity().getContent(); 
      bufR = new BufferedReader(new InputStreamReader(is)); 
      String ligneLue = bufR.readLine(); 

      while(ligneLue != null){ 
       stringB.append(ligneLue); 
       stringB.append("\n"); 
       ligneLue = bufR.readLine(); 
      } 

     }catch(Exception e){ 
       e.printStackTrace(); 
      }finally{ 
       if(bufR != null){ 
        try{ 
        bufR.close(); 
        }catch(IOException ioe){ 
         ioe.printStackTrace(); 

        } 
       } 
      } 
     try{ 
      JSONArray jArray = new JSONArray(stringB.toString()); 
      for(int i = 0; i<jArray.length(); i++){ 
       oActualite = new Actualite(); 
       oActualite.setoContenu(jArray.getJSONObject(i).getString("contenu").toString()); 
       oActualite.setoChemin(jArray.getJSONObject(i).getString("chemin").toString()); 
       oActualite.setoDate(jArray.getJSONObject(i).getString("date").toString()); 

       this.listeActualites.add(oActualite); 


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

     } 

     return listeActualites; 
      } 

     // Executed after the complete execution of doInBackground() method 
      @Override 
      protected void onPostExecute(ArrayList<Actualite> listeActualites){ 


       ActualiteAdapter adapter = new ActualiteAdapter(Actu_Connextion.this, listeActualites); 
       ListView listeVueActualites = (ListView) findViewById(R.id.lstActualites); 
       listeVueActualites.setAdapter(adapter); 

       } 

      } 

     } 
+2

什麼是致命異常?給我們堆棧跟蹤,這樣我們可以提供幫助。 – Geros

+0

了java.lang.RuntimeException:在執行doInBackground時出錯() 在android.os.AsyncTask $ 3.done(AsyncTask.java:278) 在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask。 java:273) – Predator

回答

0

我想你可能是一個null對象上調用add一個MySQL數據庫的內容......這是在這裏:

this.listeActualites.add(oActualite); 

哪裏listeActualites指的是當地的我登錄AsyncTask,你永遠不會初始化它。

+0

謝謝您的回覆,我是新的領域可以更詳細一個請謝謝 – Predator

+0

初始化您的asynctacks的listeActualites成員與初始化聲明像ArrayList listeActualites =新的ArrayList 並查看錯誤是還在那兒。對於定義爲AsyncTask成員的listeActualites,我們必須這樣做(因爲我發現在Activity中也定義了一個名字相同的成員)。 –

+0

非常感謝你;) – Predator