2013-12-14 41 views
0

這是我的logcatNullPointerException異常不對勁pDialog.dismiss()

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":"[email protected]","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 = "[email protected]"; 
//   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中聲明瞭空文本顯示。

這裏的問題,當我刪除onPostExecute方法pDialog.dismiss();,沒有更多的錯誤,但我希望對話框自行解散,而不是用戶點擊後退按鈕關閉對話框。

回答

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

嘗試。

+0

就是這樣,我覺得我宣佈兩次進展pDialog ..一個在我的CheckCard類,另一個onPreExecute方法。謝謝你的幫助 –

+0

是的,所以當你聲明成員變量時,你可以這樣聲明。進度mPDialog。 – venciallee

+0

Progress和ProgressDialog有何不同?如果我聲明進步mPDialog它只適用於該方法? –

0

您尚未將值設置爲pDialog。 在onPreExecute()

ProgressDialog pDialog = new ProgressDialog(CheckCard.this); 

應改爲

pDialog = new ProgressDialog(CheckCard.this); 
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(); 
} 
+1

我在我的CheckCard類中聲明'private ProgressDialog pDialog;',然後我聲明'ProgressDialog pDialog = new ProgressDialog(CheckCard.this);'再次在onPreexecute方法中混淆了應用程序,這就是我的想法 –