使用Handler Asynctask更新UI

时间:2014-03-27 22:13:42

标签: android android-asynctask android-handler

我问这个前1小时: Update ListView Textview vom Asyntask

现在我有下一个问题。我试图用处理程序解决问题:

我从我的自定义ListViewAdapter调用我的Asyntask:

Bundle bndl = new Bundle();
bndl.putLong("startTime", 66);
timeCounter counter = new timeCounter(this, holder.txtTimeTo);
counter.execute(bndl);

这是我的Asynctask:

public class timeCounter extends AsyncTask<Bundle, Void, String> {
private String jsonResult;
ArrayList<club> clublist= new ArrayList<club>();
private String url = "http://ivenda.de/nyte/getclubs.php";
public ticketAdapter actv;
private final Handler mHandler = new Handler();
TextView txtTime = null;


public timeCounter(ticketAdapter ac, TextView txtTime)
{
    actv = ac;
}

@Override
protected String doInBackground(Bundle... getData) {
    long restTime = getData[0].getLong("startTime");
    while(restTime>0) {
        SystemClock.sleep(1000);
        restTime = restTime - 1;
        setTime(restTime);
    }

    return null;
}


public void setTime(final long restTime) {
    final functions func = new functions();
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            txtTime.setText(func.getTimeText(restTime));

        }
    }, 0);
}
}

等待1秒后我收到这个错误:

03-27 22:09:16.696: E/AndroidRuntime(4154): FATAL EXCEPTION: main   
03-27 22:09:16.696: E/AndroidRuntime(4154): java.lang.NullPointerException
03-27 22:09:16.696: E/AndroidRuntime(4154):     at                   
ivenda.pack.timeCounter$1.run(timeCounter.java:68)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at       
android.os.Handler.handleCallback(Handler.java:725)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at         android.os.Handler.dispatchMessage(Handler.java:92)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at android.os.Looper.loop(Looper.java:137)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at java.lang.reflect.Method.invoke(Method.java:511)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 22:09:16.696: E/AndroidRuntime(4154):     at dalvik.system.NativeStart.main(Native Method)

有人有什么想法吗?

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:3)

您的TextView txtTime为空。您需要告诉它要更新哪个TextView