NullPointerException:尝试对空对象引用调用虚拟方法 AlertDialog.setTitle(java.lang.CharSequence)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29383350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NullPointerException: Attempt to invoke virtual method AlertDialog.setTitle(java.lang.CharSequence) on a null object reference
提问by manikanta g
Here is the error:
这是错误:
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)
04-01 10:37:47.077 2310-2326/zonup.asyc D/Create Response: {"success":"false","msg":"请输入电子邮件!!!"} 04-01 10:37:47.084
2310-2310/zonup.asyc D/AndroidRuntime:关闭VM 04-01 10:37:47.084 2310-2310/zonup.asyc E/AndroidRuntime:致命异常:主进程:zonup.asyc,PID:2310 java.lang. NullPointerException:尝试在 zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:122) 处的空对象引用上调用虚拟方法“void android.app.AlertDialog.setTitle(java.lang.CharSequence)” .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) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 在 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();
}
}
}
采纳答案by Shvet
Rather using AlertDialog use AlertDialog.Builder. As AlertDialog is deprected. Also you need to show progressdialog too.
而是使用 AlertDialog 使用 AlertDialog.Builder。由于 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();
}
In onPostExecute
method dismiss
the progressbar. Also Use AlertDialog.Builder.
在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();
});
回答by Tanner Perrien
You have a null pointer exceptionon dialog.setTitle("Info")
but it appears you really should be calling pDialog.setTitle("Info")
since the dialog you initialized in onPreExecute
is pDialog
rather than alertDialog
你有一个空指针异常的dialog.setTitle("Info")
,但似乎你真的应该打电话pDialog.setTitle("Info")
,因为你在初始化的对话框onPreExecute
是pDialog
不是alertDialog
回答by Pong Petrung
You Try Codeing you
你试着编码你
you try in method onPreExecute
您尝试使用 onPreExecute 方法
private ProgressDialog PD;
if(PD == null){
//
PD = new ProgressDialog(SplashScreenActivity.this);
PD.setMessage("Please Wait..");
PD.setIndeterminate(false);
PD.setCancelable(true);
PD.show();
}
and you cloce PrpgressDialog in method onPostExecute
然后在 onPostExecute 方法中关闭 PrpgressDialog
if(PD.isShowing()){
PD.dismiss();
goNext();
}