[英]Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
i got an error Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
everytime i build my app每次我构建我的应用程序时,我都会Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
错误
here's code of my error这是我的错误代码
07-26 15:04:32.587 18897-18915/com.example.study E/AndroidRuntime: FATAL EXCEPTION: Thread-385
Process: com.example.study, PID: 18897
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
at com.example.study.Splash.checking(Splash.java:66)
at com.example.study.Splash$2.run(Splash.java:51)
it happened on my splash activity, here's my code它发生在我的飞溅活动中,这是我的代码
package com.example.study;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import com.example.study.helper.SessionManager;
import com.example.study.util.ConnectionDetector;
public class Splash extends AppCompatActivity {
private ConnectionDetector cd;
Boolean isInternetPresent = false;
protected SessionManager session;
private AlertDialog.Builder builder;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);
session = new SessionManager(getApplicationContext());
cd = new ConnectionDetector(getApplicationContext());
builder.setTitle("No Connection");
builder.setMessage("Check Your Internet Connection.");
builder.setIcon(R.drawable.fail);
builder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
/* TODO Auto-generated method stub */
dialog.dismiss();
}
});
Thread timer = new Thread(){
public void run(){
try {
sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
checking();
}
}
};
timer.start();
}
public void checking() {
isInternetPresent = cd.isConnectingToInternet();
if(isInternetPresent) {
session.checkLogin();
finish();
} else {
builder.create().show();
finish();
}
}
}
dunno what to do ... please help to solve this problem,不知道该怎么办...请帮忙解决这个问题,
I think it fails at your checking()
method.我认为它在您的checking()
方法中失败了。 You have declared a global variable named builder
, then you declared another variable inside your onCreate()
.您已经声明了一个名为builder
的全局变量,然后在您的onCreate()
声明了另一个变量。 In your checking()
method, it refers to the global variable which you didn't initialize, only declare.在您的checking()
方法中,它指的是您未初始化、仅声明的全局变量。
Possible solution, edit this :可能的解决方案,编辑这个:
AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);
to到
builder = new AlertDialog.Builder(Splash.this);
Please use this below code for reference, it's working fine for me.请使用下面的代码作为参考,它对我来说很好用。
Before onCreate()在 onCreate() 之前
AlertDialog.Builder alertDialog;
Within OnCreate()在 OnCreate() 内
alertDialog = new AlertDialog.Builder(YourActivity.this);
Create AlertDialog创建警报对话框
alertDialog.setMessage("Do you want to continue ?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//write your code here after click yes
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.