3

我正在編寫一個從數據庫下載數據的程序,並且當我使用onPostExecute方法處理精細的數據時,一切正常。因爲我在調用AsynkTask類的類中需要這些數據,所以我使用了方法new AsynkTaskClass.execute().get(),它工作正常。最終我實現了一個接口,而不是.get()方法,程序崩潰。當我調試它時,它在方法updateJSONData()的一行中崩潰,在doInBackground中調用,奇怪的部分是在該方法中,當我實現接口時,我沒有改變行。我實現的接口爲this answer.使用AsynkTask和接口崩潰的Android程序

我把這裏的一些代碼段:

Register.java:從我稱之爲擴展AsynkTask類的類。

package com.PacchettoPrincipale.app; 

import [...] 

public class Register extends Activity implements OnClickListener{ 

    private EditText user, pass, codDoc; 
    private Button mRegister; 

    private ProgressDialog pDialog; 

    // Variabile utilizzata per ricevere le fermate attraverso l'interfaccia retrieveHashMapFromAsynkTask 
    ArrayList<HashMap<String, String>> fermate; 

    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 

    //testing on Emulator: 
    private static final String LOGIN_URL = "http://10.0.2.2/PrimaAppAndroid/register.php"; 

    //ids 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "message"; 


    //not important 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.register); 

     user = (EditText)findViewById(R.id.username); 
     pass = (EditText)findViewById(R.id.password); 
     codDoc = (EditText)findViewById(R.id.codDoc); 

     mRegister = (Button)findViewById(R.id.register); 
     mRegister.setOnClickListener(this); 
    } 


    @Override 
    public void onClick(View v) { 

     // HERE is where I call the AsynkTask class 
     new LoadFermate(Register.this, new retrieveHashMapFromAsynkTask() { 
      @Override 
      public void assignment(ArrayList<HashMap<String, String>> returnedValues) { 
       fermate = returnedValues; 
      } 
     }).execute(); 

     for (HashMap<String, String> fermata:fermate){ 
      Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    // is another AsynkTask class, don't look at it. 
    class CreateUser extends AsyncTask<String, String, String> {[...]} 

} 

LoadFermate.java是一個擴展AsynkTask類,我把一個大寫的註釋在行在程序崩潰。

package com.PacchettoPrincipale.app; 

import [...] 

public class LoadFermate extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> { 

    private Context context; 
    private retrieveHashMapFromAsynkTask retrieveListener; 

    public LoadFermate(Context context, retrieveHashMapFromAsynkTask retrieveListener){ 
     this.context = context; 
     this.retrieveListener = retrieveListener; 
    } 

    private ProgressDialog pDialog; 

    private JSONArray mComments = null; 
    private ArrayList<HashMap<String, String>> mCommentList; 

    //testing on Emulator: 
    private static final String DOWNLOAD_FERMATE_URL = "http://10.0.2.2/PrimaAppAndroid/get/fermate.php"; 

    //JSON IDS: 
    private static final String TAG_COD = "CodFermata"; 
    private static final String TAG_POSTS = "posts"; 
    private static final String TAG_NOME = "Nome"; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(context); 
     pDialog.setMessage("Download fermate..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected ArrayList<HashMap<String, String>> doInBackground(Void... voids) { 
     updateJSONData(); 
     return mCommentList; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> hashMaps) { 
     super.onPostExecute(hashMaps); 

     pDialog.dismiss(); 

     if(retrieveListener != null){ 
      retrieveListener.assignment(hashMaps); 
     } 
    } 

    private void updateJSONData() { 

     mCommentList = new ArrayList<HashMap<String, String>>(); 

     JSONParser jParser = new JSONParser(); 

     //THE PROGRAM CRASHES AT THIS LINE 
     JSONObject json = jParser.getJSONFromUrl(DOWNLOAD_FERMATE_URL); 

     try { 

      mComments = json.getJSONArray(TAG_POSTS); 

      for (int i = 0; i < mComments.length(); i++) { 
       JSONObject c = mComments.getJSONObject(i); 

       int CodFermata = c.getInt(TAG_COD); 
       String Nome = c.getString(TAG_NOME); 

       HashMap<String, String> map = new HashMap<String, String>(); 

       map.put(TAG_COD, String.valueOf(CodFermata)); 
       map.put(TAG_NOME, Nome); 

       mCommentList.add(map); 
      } 

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


    } 
} 

retrieveHashMapFromAsynkTask.java:是的接口。

package com.PacchettoPrincipale.app; 

import java.util.ArrayList; 
import java.util.HashMap; 

public interface retrieveHashMapFromAsynkTask { 
    public void assignment(ArrayList<HashMap<String, String>> returnedValues); 
} 

我也把由字Exception,那裏有許多NullPointerException filtred的logcat的。我認爲這是關鍵,但我不明白它在哪裏升起。

logcat的

01-05 05:46:18.460  52-52/? W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Landroid/media/videoeditor/MediaArtistNativeHelper; 
01-05 05:47:31.270  382-397/system_process W/BroadcastQueue﹕ Failure sending broadcast Intent { act=android.intent.action.CONFIGURATION_CHANGED flg=0x70000010 } 
    android.os.TransactionTooLargeException 
      at android.os.BinderProxy.transact(Native Method) 
      at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1059) 
      at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:421) 
      at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:507) 
      at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:555) 
      at com.android.server.am.BroadcastQueue$1.handleMessage(BroadcastQueue.java:141) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870) 
01-05 05:47:31.290  382-397/system_process W/BroadcastQueue﹕ Failure sending broadcast Intent { act=android.intent.action.CONFIGURATION_CHANGED flg=0x70000010 } 
    android.os.TransactionTooLargeException 
      at android.os.BinderProxy.transact(Native Method) 
      at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1059) 
      at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:421) 
      at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:507) 
      at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:555) 
      at com.android.server.am.BroadcastQueue$1.handleMessage(BroadcastQueue.java:141) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870) 
01-05 05:47:31.310  382-397/system_process W/BroadcastQueue﹕ Failure sending broadcast Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS flg=0x50000010 (has extras) } 
    android.os.TransactionTooLargeException 
      at android.os.BinderProxy.transact(Native Method) 
      at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1059) 
      at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:421) 
      at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:507) 
      at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:555) 
      at com.android.server.am.BroadcastQueue$1.handleMessage(BroadcastQueue.java:141) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870) 
01-05 05:47:42.750  382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '18 interface fwmark exempt add 10.0.2.2/32' failed with '400 18 Failed to add exemption rule (File exists)' 
01-05 05:47:42.870  382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '20 interface fwmark exempt add 10.0.2.3/32' failed with '400 20 Failed to add exemption rule (File exists)' 
01-05 05:47:42.920  382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '21 interface route add eth0 secondary 10.0.2.2 32 0.0.0.0' failed with '400 21 ip route modification failed (No such device)' 
01-05 05:47:42.960  382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '22 interface route add eth0 secondary 0.0.0.0 0 10.0.2.2' failed with '400 22 ip route modification failed (No such device)' 
01-05 05:47:52.480  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '30 interface fwmark exempt add 10.0.2.2/32' failed with '400 30 Failed to add exemption rule (File exists)' 
01-05 05:47:52.720  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '32 interface fwmark exempt add 173.194.40.6/32' failed with '400 32 Failed to add exemption rule (File exists)' 
01-05 05:47:55.950  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '34 interface fwmark exempt add 10.0.2.2/32' failed with '400 34 Failed to add exemption rule (File exists)' 
01-05 05:47:56.080  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '36 interface fwmark exempt add 173.194.40.0/32' failed with '400 36 Failed to add exemption rule (File exists)' 
01-05 05:47:57.240  663-678/com.android.systemui W/Binder﹕ Binder call failed. 
    android.os.DeadObjectException 
      at android.os.BinderProxy.transact(Native Method) 
      at android.content.IIntentReceiver$Stub$Proxy.performReceive(IIntentReceiver.java:124) 
      at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:816) 
      at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:394) 
      at android.os.Binder.execTransact(Binder.java:404) 
      at dalvik.system.NativeStart.run(Native Method) 
01-05 05:47:57.270  663-678/com.android.systemui E/JavaBinder﹕ *** Uncaught remote exception! (Exceptions are not yet supported across processes.) 
    java.lang.RuntimeException: android.os.DeadObjectException 
      at android.os.Parcel.writeException(Parcel.java:1366) 
      at android.os.Binder.execTransact(Binder.java:410) 
      at dalvik.system.NativeStart.run(Native Method) 
    Caused by: android.os.DeadObjectException 
      at android.os.BinderProxy.transact(Native Method) 
      at android.content.IIntentReceiver$Stub$Proxy.performReceive(IIntentReceiver.java:124) 
      at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:816) 
      at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:394) 
      at android.os.Binder.execTransact(Binder.java:404) 
            at dalvik.system.NativeStart.run(Native Method) 
01-05 05:47:59.220  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '38 interface fwmark exempt add 10.0.2.2/32' failed with '400 38 Failed to add exemption rule (File exists)' 
01-05 05:47:59.320  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '40 interface fwmark exempt add 173.194.40.5/32' failed with '400 40 Failed to add exemption rule (File exists)' 
01-05 05:48:02.400  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '42 interface fwmark exempt add 10.0.2.2/32' failed with '400 42 Failed to add exemption rule (File exists)' 
01-05 05:48:02.480  382-659/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '44 interface fwmark exempt add 173.194.40.7/32' failed with '400 44 Failed to add exemption rule (File exists)' 
01-05 05:57:13.340 1104-1104/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a5cba8) 
01-05 05:57:22.380 1104-1104/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.PacchettoPrincipale.app, PID: 1104 
    java.lang.NullPointerException 
      at com.PacchettoPrincipale.app.Register.onClick(Register.java:87) 
      at android.view.View.performClick(View.java:4438) 
      at android.view.View$PerformClick.run(View.java:18422) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
01-05 05:58:03.480  382-571/system_process W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 1104 uid 10052 
01-05 05:58:03.500  530-546/com.android.inputmethod.latin W/Binder﹕ Caught a RuntimeException from the binder stub implementation. 
    java.lang.NullPointerException 
      at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280) 
      at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129) 
      at android.os.Binder.execTransact(Binder.java:404) 
      at dalvik.system.NativeStart.run(Native Method) 
01-05 05:58:16.800  382-575/system_process W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 1136 uid 10052 
01-05 05:58:16.890  530-543/com.android.inputmethod.latin W/Binder﹕ Caught a RuntimeException from the binder stub implementation. 
    java.lang.NullPointerException 
      at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280) 
      at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129) 
      at android.os.Binder.execTransact(Binder.java:404) 
      at dalvik.system.NativeStart.run(Native Method) 

另一個怪異的一部分是,空指針位於寄存器87行,在那裏我不會和調試來到一條線。它在到達之前崩潰:在doInBackground期間崩潰,註冊:87在onPostExecute之後執行。

感謝您提前回答!

+0

貌似這一切從這裏開始:顯示java.lang.NullPointerException 在com.PacchettoPrincipale.app.Register.onClick( Register.java:87) – marcinj

+0

我更新了問題,我忘了一部分。謝謝你提醒我吧! – giacomotb

回答

2

這裏是NullPointerException異常...

for (HashMap<String, String> fermata : fermate){ 
     Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show(); 
    } 

這裏fermate爲空。您正在初始化fermateretrieveHashMapFromAsynkTask回調,該回叫將由AsyncTask在其onPostExecute()中調用。你應該等待AsyncTask來完成它的任務或移動舉杯retrieveHashMapFromAsynkTask實現...

new LoadFermate(Register.this, new retrieveHashMapFromAsynkTask() { 
     @Override 
     public void assignment(ArrayList<HashMap<String, String>> returnedValues) { 
      fermate = returnedValues; 
      for (HashMap<String, String> fermata:fermate){ 
       Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }).execute(); 
+0

我該如何等待? – giacomotb

+0

@giacomotb看到編輯後的代碼... –

+0

只是一個建議......在我真正的項目中,我離開了Toast,我會做所有這些東西3次(我會加載'fermate','tratta'和'linea '),然後我將使用這3個數組。我如何等待所有3將被加載,並避免它的一個引發NullPointerException? – giacomotb