我正在编写一个从数据库下载数据的程序,当我使用onPostExecute
方法获取精细数据时,一切正常。因为我在调用AsynkTask类的类中需要这些数据,所以我使用方法new AsynkTaskClass.execute().get()
并且它有效。最终我实现了一个接口而不是.get()
方法,程序崩溃了。当我调试它时,它在方法updateJSONData()
的一行中崩溃,在doInBackground
中调用,而奇怪的部分是在该方法中我在实现接口时没有更改一行。我在this answer.
我在这里放了一些代码:
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);
}
我还将LogCat过滤了Exception
这个词,其中有许多NullPointerException
。我认为这是关键,但我无法理解它在哪里上升。
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)
另一个奇怪的部分是nullPointer位于寄存器第87行,这是我没有通过调试到达的行。它在到达之前崩溃:它在doInBackground
期间崩溃,而注册:87在onPostExecute
之后执行。
提前感谢您的回答!
答案 0 :(得分:2)
这是NullPointerException ...
for (HashMap<String, String> fermata : fermate){
Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show();
}
此处fermate
为空。您正在fermate
回调中初始化retrieveHashMapFromAsynkTask
,AsyncTask
将调用onPostExecute()
。AsyncTask
。您应该等待retrieveHashMapFromAsynkTask
完成任务或将Toast移至 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();
实施......
{{1}}