异步任务有时工作,大部分时间都不工作

时间:2014-08-28 05:18:52

标签: android android-asynctask

我正在学习移动应用程序开发当我实现了Async Task时,除了doInBackground之外我添加了onPostExecute和onPreExecute它没有工作,但是它在一段时间后停止工作。实际上我正在尝试从EditText中保存数据,单击“保存”按钮并尝试使用“加载”按钮将其恢复并在“文本视图”中查看。现在我还为此添加了一个进度条。它抛出空指针异常。

public class loadSomeStuff extends AsyncTask<String, Integer, String>{
        ProgressDialog dialog;

    protected void onPreExecute(String f) {
        dialog = new ProgressDialog(FileOperations.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.show();
        f="whatever";
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        FileInputStream fis=null;
        String dataResult=null;

        for(int i=0;i<20;i++){
            publishProgress(5);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        dialog.dismiss();
        try {
            fis = openFileInput(FILENAME);
            byte[] dataArray = new byte[fis.available()];
            while(fis.read(dataArray) != -1){
                dataResult= new String(dataArray);

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            try {
                fis.close();
                return dataResult;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return "no data to handle";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.incrementProgressBy(values[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        viewResults.setText(result);
    }

}
}

这是我的logcat:

08-28 00:34:42.718: W/EGL_emulation(1430): eglSurfaceAttrib not implemented
08-28 00:34:49.528: D/AndroidRuntime(1430): Shutting down VM
08-28 00:34:49.528: W/dalvikvm(1430): threadid=1: thread exiting with uncaught exception (group=0xada39ba8)
08-28 00:34:49.538: E/AndroidRuntime(1430): FATAL EXCEPTION: main
08-28 00:34:49.538: E/AndroidRuntime(1430): Process: com.tarragon.tmessenger, PID: 1430
08-28 00:34:49.538: E/AndroidRuntime(1430): java.lang.NullPointerException
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.tarragon.tmessenger.FileOperations$loadSomeStuff.onProgressUpdate(FileOperations.java:154)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.tarragon.tmessenger.FileOperations$loadSomeStuff.onProgressUpdate(FileOperations.java:1)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.os.Looper.loop(Looper.java:136)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invokeNative(Native Method)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invoke(Method.java:515)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-28 00:34:49.538: E/AndroidRuntime(1430):     at dalvik.system.NativeStart.main(Native Method)
08-28 00:34:51.598: W/dalvikvm(1430): threadid=12: thread exiting with uncaught exception (group=0xada39ba8)
08-28 00:34:51.598: I/Process(1430): Sending signal. PID: 1430 SIG: 9

4 个答案:

答案 0 :(得分:4)

您的onPreExecute()签名错误。它不应该采取任何论点。因此,它不会覆盖方法而不会被调用,因此dialog仍然为空。

另请注意缺少@Override注释 - 具有默认配置的IDE会自动将其添加到覆盖方法的方法中,而您的方法则没有。

从方法中删除String f参数。

答案 1 :(得分:2)

只需从dialog.dismiss();方法中删除doInBackground,然后将其写入onPostExecute方法即可。您收到Nullpointer错误,因为您已经解除了对话框,同时您正在尝试更新其进度。这就是它无法获得对话的原因。

我已从dialog.dismiss();移除了doInBackground方法,并将其保存在onPostExecute();

试试如下:

 @Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    FileInputStream fis=null;
    String dataResult=null;

    for(int i=0;i<20;i++){
        publishProgress(5);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
  ...........................................

    return "no data to handle";
}

@Override
protected void onProgressUpdate(Integer... values) {
    dialog.incrementProgressBy(values[0]);
}

@Override
protected void onPostExecute(String result) {
      dialog.dismiss();
    viewResults.setText(result);
}

}

答案 2 :(得分:2)

您的onPreExecute方法错误,如

ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(FileOperations.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.show();

    }

并从doInBackground中删除dialog.dismiss()并将其添加到onPostExecute方法,它会喜欢这个..

@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        viewResults.setText(result);

        if (dialog.isShowing())
            dialog.dismiss();
    }

答案 3 :(得分:0)

写这样的Post方法:

@Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
   }