Android ASyncTask publishProgress错误

时间:2012-02-18 08:13:07

标签: android android-asynctask text-files

所以这就是我遇到的问题:每当publishProgress(sentCount);在应用程序崩溃中调用。我环顾四周,仍然不知道原因。任何帮助是极大的赞赏!谢谢

class MySmsTask extends AsyncTask<String, Integer, Integer> {

    protected ProgressBar onPreExecute(String...messageData) {
        final ProgressBar progress = progressSend;
        int msgCount = Integer.parseInt(messageData[2]);
        progress.setMax(msgCount);
        progress.setProgress(0);
        return progress;
    }   
    @Override
    protected Integer doInBackground(String... messageData) {
        int sentCount = 0;
        int size = 1;
        int delay = Integer.parseInt(messageData[3]);
        int delayf = delay + 2;
        int msgCount = Integer.parseInt(messageData[2]);
        String message = messageData[1];
        String phoneNo = messageData[0];
        if (phoneNo.length()>0 && message.length()>0){
            for (sentCount = 0; sentCount < msgCount; sentCount++) {
                for (int i = 0; i < size; i++) {
                    sendSMS(messageData[0], messageData[1]);

                    try {
                        publishProgress(sentCount);
                        Thread.sleep(delayf * 1000);

                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }
                }
            }

        }
        return sentCount;

    }
    protected void onProgressUpdate(Integer... mprogress) {
        progress.setProgress(mprogress[0]);
        /*Toast.makeText(getBaseContext(), "Sent " + mprogress + " messages", Toast.LENGTH_SHORT).show();*/
    }

    protected void onPostExecute(Integer result) {
        Toast.makeText(getBaseContext(), "Sent " + result + " messages", Toast.LENGTH_SHORT).show();
    }

ERROR:

02-18 00:04:44.019: E/AndroidRuntime(32561): FATAL EXCEPTION: main
02-18 00:04:44.019: E/AndroidRuntime(32561): java.lang.NullPointerException
02-18 00:04:44.019: E/AndroidRuntime(32561):    at          txt.messager.app.TxtMessagerActivity$MySmsTask.onProgressUpdate(TxtMessagerActivity.java:85)
02-18 00:04:44.019: E/AndroidRuntime(32561):    at txt.bomber.app.TxtMessagerActivity$MySmsTask.onProgressUpdate(TxtMessagerActivity.java:1)
02-18 00:04:44.019: E/AndroidRuntime(32561):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
02-18 00:04:44.019: E/AndroidRuntime(32561):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 00:04:44.019: E/AndroidRuntime(32561):    at android.os.Looper.loop(Looper.java:130)
02-18 00:04:44.019: E/AndroidRuntime(32561):    at android.app.ActivityThread.main(ActivityThread.java:3906)
02-18 00:04:44.019: E/AndroidRuntime(32561):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 00:04:44.019: E/AndroidRuntime(32561): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 00:04:44.019: E/AndroidRuntime(32561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
02-18 00:04:44.019: E/AndroidRuntime(32561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
02-18 00:04:44.019: E/AndroidRuntime(32561):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

progress中设置的onPreExecute是一个局部变量。我不知道编译器在progress中看到了哪个onProgressUpdate,但它不是同一个。因此,它可能是空的。