NullPointerException:尝试对null对象引用调用虚拟方法AlertDialog.setTitle(j



以下是错误:

04-01 10:37:47.077 2310-2326/zonup.asyc D/创建响应﹕{"成功":"错误","消息":"请输入电子邮件!!"}04-01 10:37:47.084
2310-2310/zonup.asyc D/AndroidRuntime﹕关闭VM 04-0110:37:47.084 2310-2310/zonup.asyc E/AndroidRuntime﹕致命的例外:main进程:zonup.asyc,PID:2310java.lang.NullPointerException:尝试在空对象引用位于zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:122)位于zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:70)在android.os.AsyncTask.fining(AsyncTask.java:632)在android.os.AsyncTask.access上$600(AsyncTask.java:177)在android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5221)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.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();
    }
}
}

您在dialog.setTitle("Info")上有一个空指针异常,但您似乎真的应该调用pDialog.setTitle("Info"),因为在onPreExecute中初始化的对话框是pDialog而不是alertDialog

您尝试对进行编码

您在PreExecute 上尝试方法

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

然后在方法onPostExecute 中关闭PrpgressDialog

if(PD.isShowing()){
                    PD.dismiss();
                    goNext();
                }

使用AlertDialog.Builder而不是使用AlertDialog。因为AlertDialog被弃用。此外,您还需要显示进度对话框。

 @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();
        });

相关内容

最新更新