Android:NullPointerException onProgressUpdate

时间:2014-08-13 19:38:10

标签: java android android-asynctask nullpointerexception dialog

好的我想尝试创建progressDialog但是当我运行我的代码时会出现NullPointerException。现在在良好的旧logcat的帮助下,我可以确认我的对话框为空但是我无法弄清楚为什么会这样。继承我的代码

   public class asyncTask extends AsyncTask<String, Integer, String> {

        ProgressDialog dialog;

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

         @Override
           protected String doInBackground(String...parmans){
                {

                    for(int i = 0; i < 20; i++){

                        Log.v(TAG, "index=" + "loop");
                        publishProgress(5);
                        Log.v(TAG, "index=" + dialog);
                    }

                    String urlString = petrolPriceURL;
                    String result = "";
                    InputStream anInStream = null;
                    int response = -1;
                    URL url = null;

                    try {
                        url = new URL(urlString);
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        return null;
                    }
                    URLConnection conn = null;
                    try {
                        conn = url.openConnection();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        return null;
                    }

                    // Check that the connection can be opened
                    if (!(conn instanceof HttpURLConnection))
                        try {
                            throw new IOException("Not an HTTP connection");
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            return null;
                        }
                    try
                    {
                        // Open connection
                        HttpURLConnection httpConn = (HttpURLConnection) conn;
                        httpConn.setAllowUserInteraction(false);
                        httpConn.setInstanceFollowRedirects(true);
                        httpConn.setRequestMethod("GET");
                        httpConn.connect();
                        response = httpConn.getResponseCode();
                        // Check that connection is OK
                        if (response == HttpURLConnection.HTTP_OK)
                        {
                            // Connection is OK so open a reader 
                            anInStream = httpConn.getInputStream();
                            InputStreamReader in= new InputStreamReader(anInStream);
                            BufferedReader bin= new BufferedReader(in);

                            // Read in the data from the RSS stream
                            String line = new String();
                            while (( (line = bin.readLine())) != null)
                            {
                                result = result + "\n" + line;
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                            try {
                                throw new IOException("Error connecting");
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    }

            return result;
                }
           }
           @Override

           protected void onProgressUpdate(Integer...progress){

               dialog.incrementProgressBy(progress[0]);
           }

           @Override
           protected void onPostExecute(String result) {
               // Get the data from the RSS stream as a string

               dialog.dismiss();

               response = (TextView)findViewById(R.id.error);
               response = (TextView)findViewById(R.id.title);

               try
                {
                    // Get the data from the RSS stream as a string
                    result =  doInBackground(petrolPriceURL);

                    response.setText(result);
                    Log.v(TAG, "index=" + result);
                }
                catch(Exception ae)
                {
                    // Handle error
                    response.setText("Error");
                    // Add error info to log for diagnostics
                    errorText.setText(ae.toString());
                } 


           }

当publishProgress(5);发生我的logcat告诉我变量对话框之后仍然为null所以我意识到我的错误必须与onPreExecute和对话框的初始化有关,或者我在onPostUpdate中增加了一些问题但这似乎不太可能。这也是我的logcat,它说明了我的问题,并显示我的对话框为空。

  08-13 15:35:01.976: V/AreaURL(1676): index=http://www.petrolprices.com/feeds/averages.xml?search_type=&search_value=
08-13 15:35:03.136: V/PetrolPrice(1676): index=loop
08-13 15:35:03.136: V/PetrolPrice(1676): index=null
08-13 15:35:03.136: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.146: V/PetrolPrice(1676): index=null
08-13 15:35:03.146: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.156: V/PetrolPrice(1676): index=loop
08-13 15:35:03.156: V/PetrolPrice(1676): index=null
08-13 15:35:03.286: D/AndroidRuntime(1676): Shutting down VM
08-13 15:35:03.286: W/dalvikvm(1676): threadid=1: thread exiting with uncaught exception (group=0xb3a82ba8)
08-13 15:35:03.326: E/AndroidRuntime(1676): FATAL EXCEPTION: main
08-13 15:35:03.326: E/AndroidRuntime(1676): Process: org.me.myandroidstuff, PID: 1676
08-13 15:35:03.326: E/AndroidRuntime(1676): java.lang.NullPointerException
08-13 15:35:03.326: E/AndroidRuntime(1676):     at org.me.myandroidstuff.PetrolPriceActivity$asyncTask.onProgressUpdate(PetrolPriceActivity.java:140)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at org.me.myandroidstuff.PetrolPriceActivity$asyncTask.onProgressUpdate(PetrolPriceActivity.java:1)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at android.os.Looper.loop(Looper.java:136)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invoke(Method.java:515)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-13 15:35:03.326: E/AndroidRuntime(1676):     at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

如果你在onPreExecute()之外实例化你的对话框会发生什么。像那样:

ProgressDialog dialog = new ProgressDialog(PetrolPriceActivity.this);

我不知道为什么会这样。可能是你没有进入onPreExecute x)添加日志以查看那个:)