2012-09-07 55 views
1

您看到我試圖在此適配器上加載一些數據,但我無法使AsyncTask顯示進度對話框。AsyncTask在doInBackground()中崩潰

這是它給我錯誤的程序的完整代碼。我完全修改了所以不要編輯任何UI。

public class MatchNowGames extends Activity { 
Games games = new Games(); 
Iterator iterador; 
private ProgressDialog dialog; 
GamesArrayAdapter adapter; 
ListView lv; 
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);   
    // INCIO PROGRAMA   
    setContentView(R.layout.games_list); 
    lv = (ListView) this.findViewById(R.id.gamesLV); 
    // Creamos el thread 
    dialog = new ProgressDialog(this); 
    dialog.setMessage("Cargando..."); 
    dialog.setTitle("Progreso"); 
    dialog.setCancelable(false); 

    // INIT VARIABLES 
    new CargandoJuegos().execute(); 
    adapter = new GamesArrayAdapter(App.context().getApplicationContext(), R.layout.games_list, games.getGames());  


} 
private class CargandoJuegos extends AsyncTask<Void, Void, Void>{ 
    protected void onPreExecute() { 
      dialog.show(); 
      } 

    protected Void doInBackground(Void... urls) { 
     // AÑADIMOS LOS JUEGOS 
     try { 
      games.addGames(MatchNow.serverGames); 
      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 

       e.printStackTrace(); 
      } 
     } catch (JSONException e) {} 
     return null; 
    } 
    protected void onPostExecute(Void unused) { 
     dialog.dismiss(); 
     lv.setAdapter(adapter); 
    } 


    } 

} 

這是我們應該做的,而顯示progressdialog:

// Start progress dialog 
    try { 
      games.addGames(MatchNow.serverGames); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    // Finish progress dialog 
    GamesArrayAdapter adapter = new GamesArrayAdapter(App.context().getApplicationContext(), R.layout.games_list, games.getGames()); 
    ListView lv = (ListView) this.findViewById(R.id.gamesLV); 
    lv.setAdapter(adapter); 

非常感謝你事先=)

的logcat的:

09-07 02:18:07.916: D/AndroidRuntime(890): Shutting down VM 
09-07 02:18:07.916: W/dalvikvm(890): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
09-07 02:18:07.926: E/AndroidRuntime(890): Uncaught handler: thread main exiting due to uncaught exception 
09-07 02:18:07.936: E/AndroidRuntime(890): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{match.now.play/match.now.play.MatchNowGames}: java.lang.NullPointerException 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.os.Looper.loop(Looper.java:123) 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.ActivityThread.main(ActivityThread.java:4363) 
09-07 02:18:07.936: E/AndroidRuntime(890): at java.lang.reflect.Method.invokeNative(Native Method) 
09-07 02:18:07.936: E/AndroidRuntime(890): at java.lang.reflect.Method.invoke(Method.java:521) 
09-07 02:18:07.936: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
09-07 02:18:07.936: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
09-07 02:18:07.936: E/AndroidRuntime(890): at dalvik.system.NativeStart.main(Native Method) 
09-07 02:18:07.936: E/AndroidRuntime(890): Caused by: java.lang.NullPointerException 
09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.Activity.findViewById(Activity.java:1612) 
09-07 02:18:07.936: E/AndroidRuntime(890): at match.now.play.MatchNowGames.<init>(MatchNowGames.java:40) 

我解決1612線的問題是一個疏忽。 Logcat確實給出了問題。抱歉給你帶來不便。

09-07 02:18:07.936: E/AndroidRuntime(890): at java.lang.Class.newInstanceImpl(Native Method) 
    09-07 02:18:07.936: E/AndroidRuntime(890): at java.lang.Class.newInstance(Class.java:1479) 
    09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
    09-07 02:18:07.936: E/AndroidRuntime(890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409) 
    09-07 02:18:07.936: E/AndroidRuntime(890): ... 11 more 


09-07 02:34:31.486: D/dalvikvm(930): GC freed 404 objects/23688 bytes in 60ms 
09-07 02:34:32.976: I/global(930): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
09-07 02:34:32.986: I/System.out(930): {"games":[{"id":"1","image":"http:\/\/cibermaniacos.com\/images\/stories\/call_of_duty_modern_warfare_3_desvelado_dest01.jpg","name":"Call Of Duty MW3","descrip":"","modes":[{"id":"1","name":"1vs1","mode_icon":"none"},{"id":"2","name":"2vs2","mode_icon":"none"}],"console":"PS3,XBOX"},{"id":"2","image":"http:\/\/www.hry-na-mobil.eu\/data\/ikonky\/hry-na-mobil-eu\/46801\/call-of-duty-world-at-war-codww-1000x1000jpg-12.png","name":"Prueba","descrip":"afafs","modes":[{"id":"1","name":"1vs1","mode_icon":"none"},{"id":"2","name":"2vs2","mode_icon":"none"}],"console":"steam,PS3"},{"id":"4","image":"http:\/\/cibermaniacos.com\/images\/stories\/call_of_duty_modern_warfare_3_desvelado_dest01.jpg","name":"Call Of Duty MW2","descrip":"AWDAWD","modes":[{"id":"1","name":"1vs1","mode_icon":"none"},{"id":"2","name":"2vs2","mode_icon":"none"}],"console":"steam,PS3"}]}     
09-07 02:34:33.036: I/System.out(930): Call Of Duty MW2 
09-07 02:34:33.046: I/System.out(930): Call Of Duty MW3 
09-07 02:34:33.068: W/dalvikvm(930): threadid=15: thread exiting with uncaught exception (group=0x4001b188) 
09-07 02:34:33.068: E/AndroidRuntime(930): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception 
09-07 02:34:33.086: E/AndroidRuntime(930): java.lang.RuntimeException: An error occured while executing doInBackground() 
09-07 02:34:33.086: E/AndroidRuntime(930): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.lang.Thread.run(Thread.java:1096) 
09-07 02:34:33.086: E/AndroidRuntime(930): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
09-07 02:34:33.086: E/AndroidRuntime(930): at android.os.Handler.<init>(Handler.java:121) 
09-07 02:34:33.086: E/AndroidRuntime(930): at android.app.Activity.<init>(Activity.java:673) 
09-07 02:34:33.086: E/AndroidRuntime(930): at match.now.play.functions.Game.<init>(Game.java:52) 
09-07 02:34:33.086: E/AndroidRuntime(930): at match.now.play.functions.Games.addGames(Games.java:58) 
09-07 02:34:33.086: E/AndroidRuntime(930): at match.now.play.MatchNowGames$CargandoJuegos.doInBackground(MatchNowGames.java:67) 
09-07 02:34:33.086: E/AndroidRuntime(930): at match.now.play.MatchNowGames$CargandoJuegos.doInBackground(MatchNowGames.java:1) 
09-07 02:34:33.086: E/AndroidRuntime(930): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
09-07 02:34:33.086: E/AndroidRuntime(930): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
09-07 02:34:33.086: E/AndroidRuntime(930): ... 4 more 
+0

a)只有英文b)顯示logcat –

+0

對不起,我的英文很糟糕,我盡我所能。真的,我很抱歉=( –

+0

簡單。有句子在(我猜)西班牙語。你修正並提供logcat所以現在好了。 –

回答

0

它看起來就像你doInBackground()方法唯一真正的代碼是這樣的:

games.addGames(MatchNow.serverGames); 

但是,你不告訴我們在什麼情況addGames()方法。您是否試圖在該方法中使用UI?如果是這樣,那將是一個問題。

doInBackground()不用於直接進行任何UI調用。如果在後臺處理期間UI需要更新,您通常會在doInBackground()內撥打publishProgress()觸發onProgressUpdate()。您可以在onProgressUpdate()之內進行UI調用。