2015-04-01 76 views
0

這裏調用虛擬方法AlertDialog.setTitle(java.lang.CharSequence中)是錯誤:的NullPointerException:嘗試上的空對象引用

04-01 10:37:47.077 2310-2326/zonup.asyc D/Create Response﹕ {"success":"false","msg":"Please enter email!!!"} 04-01 10:37:47.084
2310-2310/zonup.asyc D/AndroidRuntime﹕ Shutting down VM 04-01 10:37:47.084 2310-2310/zonup.asyc E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: zonup.asyc, PID: 2310 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlertDialog.setTitle(java.lang.CharSequence)' on a null object reference at zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:122) at zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:70) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

public class MainActivity extends ActionBarActivity { 

protected String stmail; 
protected EditText editText; 
protected Button button; 
AlertDialog alertDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editText=(EditText) findViewById(R.id.editText); 
    stmail=editText.getText().toString(); 
    button=(Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      new SignmeUp().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.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
class SignmeUp extends AsyncTask<String, String, String> { 

    private ProgressDialog pDialog; 
    JSONParser jsonParser = new JSONParser(); 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       pDialog = new ProgressDialog(MainActivity.this); 
       pDialog.setMessage("Thanks for your Sign up..."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(true); 
      } 
     }); 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("email", stmail)); 



     // getting JSON Object 
     // Note that create product url accepts POST method 
     JSONObject json = jsonParser.makeHttpRequest("http://api.php","GET", params); 

     // check log cat fro response 
     Log.d("Create Response", json.toString()); 

     // check for success tag 
     try { 
      String success = json.getString("success"); 

      if (stmail != null & success == "true") { 
       Toast.makeText(MainActivity.this, "Youre Email Posted..", Toast.LENGTH_SHORT).show(); 
      } else { 

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

     return null; 
    } 

    @SuppressWarnings("deprecation") 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once done 

     editText.setText(""); 
     alertDialog.setTitle("Info"); 

     alertDialog.setMessage("You have been subscribed o the "); 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // here you can add functions 

      } 
     }); 

     alertDialog.show(); 

    } 

} 
} 
+0

將AlertDialog.Builder alertDialog =新AlertDialog.Builder(MainActivity.this); above alertDialog.setTitle(「Info」);行 – 2015-04-01 05:24:12

+0

remove super.onPreExecute(); – MPG 2015-04-01 05:28:13

+0

你在哪裏初始化AlertDialog? – Shvet 2015-04-01 05:34:40

回答

0

而是使用AlertDialog使用AlertDialog.Builder。隨着AlertDialog被刪除。你也需要顯示progressdialog。

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Thanks for your Sign up..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

onPostExecute方法dismiss進度條。還使用AlertDialog.Builder。

protected void onPostExecute(String file_url) { 
     // dismiss the dialog once done 

    if(pDialog.isShowing()){ 
     pDialog.dismiss(); 
     } 
     editText.setText(""); 
     AlertDailog.Builder builder = new AlertDialog.Builder(Activity.this); 
      builder.setText(""); 
      builder.setMessege(""); 
      builder.create().show(); 
     }); 
+0

@manikanta你有沒有試過我的解決方案? – Shvet 2015-04-02 05:19:49

+0

工作謝謝@shvet – 2015-04-02 06:52:11

0

你有空指針異常dialog.setTitle("Info")但現在看來,你真的應該叫pDialog.setTitle("Info")因爲你在onPreExecute初始化的對話框pDialog而非alertDialog

0

您嘗試Codeing你

您嘗試在預執行

private ProgressDialog PD; 

     if(PD == null){ 

    // 
         PD = new ProgressDialog(SplashScreenActivity.this); 
         PD.setMessage("Please Wait.."); 
         PD.setIndeterminate(false); 
         PD.setCancelable(true); 
         PD.show(); 
       } 

和你在方法cloce PrpgressDialog onPostExecute

if(PD.isShowing()){ 
        PD.dismiss(); 
        goNext(); 
       } 
相關問題