有2个类和1个接口:Class 1 = LoginPage,Class 2 = SyncData和Interface = AsyncTaskCompleteListener。
Class 1使用Class 2与服务器同步信息。在Class 2在onPostExecute方法中发布结果信息之后,将使用Interface回调方法将结果信息发送到Class 1.当我尝试使用回调方法时,我会看到以下错误:
04-12 11:06:43.715: W/dalvikvm(24941): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-12 11:06:43.720: E/AndroidRuntime(24941): FATAL EXCEPTION: main
04-12 11:06:43.720: E/AndroidRuntime(24941): java.lang.NullPointerException
04-12 11:06:43.720: E/AndroidRuntime(24941): at com.on_d_mand.live_evenementen.SyncData.onPostExecute(SyncData.java:101)
04-12 11:06:43.720: E/AndroidRuntime(24941): at com.on_d_mand.live_evenementen.SyncData.onPostExecute(SyncData.java:1)
04-12 11:06:43.720: E/AndroidRuntime(24941): at android.os.AsyncTask.finish(AsyncTask.java:417)
04-12 11:06:43.720: E/AndroidRuntime(24941): at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-12 11:06:43.720: E/AndroidRuntime(24941): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-12 11:06:43.720: E/AndroidRuntime(24941): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 11:06:43.720: E/AndroidRuntime(24941): at android.os.Looper.loop(Looper.java:130)
04-12 11:06:43.720: E/AndroidRuntime(24941): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-12 11:06:43.720: E/AndroidRuntime(24941): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 11:06:43.720: E/AndroidRuntime(24941): at java.lang.reflect.Method.invoke(Method.java:507)
04-12 11:06:43.720: E/AndroidRuntime(24941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-12 11:06:43.720: E/AndroidRuntime(24941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-12 11:06:43.720: E/AndroidRuntime(24941): at dalvik.system.NativeStart.main(Native Method)
Class 1 onTaskComplete方法示例:
public void onTaskComplete(String result) {
//perform action after receiving the information
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
Class 2 onPostExecute方法示例:
public void onPostExecute(String RESULT) {
if(dialog.isShowing()){
dialog.dismiss();
}
callback.onTaskComplete(RESULT);
}
第3课:
package com.on_d_mand.live_evenementen;
public interface AsyncTaskCompleteListener<T> {
public void onTaskComplete(T result);
}
有谁知道我在这里做错了什么?我希望它与Classes和Interface不太复杂。
修改
此问题的解决方案是初始化Class 2构造函数中的回调对象。
答案 0 :(得分:4)
创建它时,需要将对实现侦听器的对象的引用传递给“SyncData”类。据推测,目前在你的LoginPage中你有一些看起来像这样的东西:
new SyncData().execute();
您需要将其更改为:
new SyncData(this).execute();
并向SyncData添加构造函数:
public SyncData(AsyncTaskCompleteListener<String> callback)
{
this.callback = callback;
}
如果您已有SyncData的构造函数,则需要将参数和代码添加到现有构造函数中。
答案 1 :(得分:2)
对话框对象可能会获得null
代替
if(dialog.isShowing()){
dialog.dismiss();
试
if(dialog!=null){
dialog.dismiss();
答案 2 :(得分:1)
public void onPostExecute(String RESULT) {
if(dialog.isShowing()){
dialog.dismiss();
}
callback.onTaskComplete(RESULT); //make sure callback is not null
// and proper null check is required in onTaskComplete in case RESULT is null
}
答案 3 :(得分:0)
对话框为null或回调为空。
查看行号,您就会知道它是什么。
答案 4 :(得分:0)
试试这段代码:::
public void onTaskComplete(String result) {
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
//perform action after receiving the information
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
});
}