我将asynctask分隔为单独的类,但是现在我有错误,将结果传回去。我想使用接口来使用回调功能。你能告诉我哪里出错吗? (line 40 is callback.onDeleteAdd(result);
)
接口
public interface DeleteAddInterface {
public void onDeleteAdd(JSONObject result);
}
的AsyncTask
public class DeleteAdd extends AsyncTask<Advert, Void, JSONObject> {
private DeleteAddInterface callback;
private int atempt = 0;
private Advert advert;
public DeleteAdd(int atempt) {
this.atempt = atempt;
}
public DeleteAdd() {
}
@Override
protected JSONObject doInBackground(Advert... advert) {
this.advert = advert[0];
if (atempt < 5) {
UserFunctions u = new UserFunctions();
return u.getNewAdd(advert[0].getId());
} else
return null;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
if (result == null) {
DeleteAdd add = new DeleteAdd(atempt + 1);
add.execute(advert);
} else {
callback.onDeleteAdd(result);
}
}
}
活性
public class AddToCheckActivity extends ErrorActivity implements DeleteAddInterface{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DeleteAdd dAdd = new DeleteAdd();
dAdd.execute(adv);
@Override
public void onDeleteAdd(JSONObject result) {
Intent i = new Intent(getApplicationContext(),
MyAddPrepareActivity.class);
startActivity(i);
finish();
}
}
错误
10-27 02:16:26.322: E/AndroidRuntime(12664): FATAL EXCEPTION: main
10-27 02:16:26.322: E/AndroidRuntime(12664): java.lang.NullPointerException
10-27 02:16:26.322: E/AndroidRuntime(12664): at networkOperations.DeleteAdd.onPostExecute(DeleteAdd.java:40)
10-27 02:16:26.322: E/AndroidRuntime(12664): at networkOperations.DeleteAdd.onPostExecute(DeleteAdd.java:1)
10-27 02:16:26.322: E/AndroidRuntime(12664): at android.os.AsyncTask.finish(AsyncTask.java:631)
10-27 02:16:26.322: E/AndroidRuntime(12664): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-27 02:16:26.322: E/AndroidRuntime(12664): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-27 02:16:26.322: E/AndroidRuntime(12664): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 02:16:26.322: E/AndroidRuntime(12664): at android.os.Looper.loop(Looper.java:158)
10-27 02:16:26.322: E/AndroidRuntime(12664): at android.app.ActivityThread.main(ActivityThread.java:5751)
10-27 02:16:26.322: E/AndroidRuntime(12664): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 02:16:26.322: E/AndroidRuntime(12664): at java.lang.reflect.Method.invoke(Method.java:511)
10-27 02:16:26.322: E/AndroidRuntime(12664): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-27 02:16:26.322: E/AndroidRuntime(12664): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-27 02:16:26.322: E/AndroidRuntime(12664): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您需要将侦听器(您的Activity)的实现传递给AsyncTask。更改您的构造函数:
public DeleteAdd(DeleteAddInterface callback) {
this.callBack = callback;
}
和你的活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DeleteAdd dAdd = new DeleteAdd(this);
dAdd.execute(adv);
欢呼声