NullPointerException错误pDialog.dismiss()

时间:2013-12-14 03:05:34

标签: android nullpointerexception

这是我的logcat

12-14 10:46:52.089: D/skia(6391): ---- fAsset->read(2202) returned 0
12-14 10:46:52.119: D/request!(6391): starting
12-14 10:46:52.569: D/Retrieve attempt(6391): {"posts":[[{"position":"user","address":"Bunga Raya","mobilenumber":"0124331292","email":"jiaweitan05@gmail.com","name":"Jiawei","state":"Melaka","userid":"1","postcode":"75450","password":"123456abcd","cardbalance":"20","city":"Bukit Beruang"}]],"message":"Post Available!","success":1}
12-14 10:46:52.569: D/Retrieve Successful!(6391): res: Post Available!
12-14 10:46:52.569: D/AndroidRuntime(6391): Shutting down VM
12-14 10:46:52.569: W/dalvikvm(6391): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-14 10:46:52.579: E/AndroidRuntime(6391): FATAL EXCEPTION: main
12-14 10:46:52.579: E/AndroidRuntime(6391): java.lang.NullPointerException
12-14 10:46:52.579: E/AndroidRuntime(6391):     at com.pmss.CheckCard$RetrieveUser.onPostExecute(CheckCard.java:158)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at com.pmss.CheckCard$RetrieveUser.onPostExecute(CheckCard.java:1)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at android.os.AsyncTask.finish(AsyncTask.java:417)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at android.os.Looper.loop(Looper.java:130)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at java.lang.reflect.Method.invoke(Method.java:507)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-14 10:46:52.579: E/AndroidRuntime(6391):     at dalvik.system.NativeStart.main(Native Method)

第158行:pDialog.dismiss();

这是我的CheckCardBalance.java

public class CheckCard extends ActionBarActivity {

    TextView checkbalance;

    private ProgressDialog pDialog;
    private JSONArray cardbalance = null;   

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    private static final String USERINFO_URL = "http://192.168.1.10:1234/PMSS/userinfo.php";
    // ids
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_CARDBALANCE = "cardbalance";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_card);

        checkbalance = (TextView) findViewById(R.id.checkbalance);
//      checkbalance.setKeyListener(null);
        //This make the textbox to grey color which means you can do nothing to it
//      checkbalance.setEnabled(false);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    protected void onResume() {
        // TODO Auto-generated method stub

        super.onResume();
        // loading the comments via AsyncTask
        new RetrieveUser().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.check_card, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }





    public class RetrieveUser extends AsyncTask<Void, Void, Boolean> {

        int success;
        String message;
        String CardBalance = null;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressDialog pDialog = new ProgressDialog(CheckCard.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... arg0) {
            String post_username = "jiaweitan05@gmail.com";
//          SharedPreferences sp = PreferenceManager
//                  .getDefaultSharedPreferences(CheckCard.this);
//          String post_username = sp.getString("username", "anon");

            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("email", post_username));


                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(USERINFO_URL, "POST",
                        params);
                Log.d("Retrieve attempt", json.toString());

                // I know I said we would check if "Posts were Avail."
                // (success==1)
                // before we tried to read the individual posts, but I lied...
                // mComments will tell us how many "posts" or comments are
                // available
                success =json.getInt(TAG_SUCCESS);
                message= json.getString(TAG_MESSAGE);
                cardbalance = json.getJSONArray(TAG_POSTS);

                Log.d("Retrieve Successful!", "res: " + message);
                JSONArray jr = cardbalance.getJSONArray(0);
                JSONObject jb = jr.getJSONObject(0);
                CardBalance = jb.getString(TAG_CARDBALANCE);


            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            Log.d("Card Balance Updated!", message);
            checkbalance.setText(CardBalance);

        }
    }


}

据我所知,NullPointerException(NPE)但是当我想在checkbalance as in TextView上显示文本时,我在activity_check_card.xml中声明了空文本显示。

问题是,当我在pDialog.dismiss();方法中删除onPostExecute时,没有更多错误,但我想让对话框自行解除,而不是用户点击后退按钮关闭对话框。

3 个答案:

答案 0 :(得分:1)

 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CheckCard.this);
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

试试这个。

答案 1 :(得分:0)

您尚未将值设置为pDialog。 在onPreExecute()

ProgressDialog pDialog = new ProgressDialog(CheckCard.this);

应改为

pDialog = new ProgressDialog(CheckCard.this);

答案 2 :(得分:0)

当您声明pDialog时,您将其声明为onPreExecute的本地变量。您需要初始化该字段,而不是声明一个新变量:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(CheckCard.this);
    pDialog.setMessage("Loading...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}