![](/img/trans.png)
[英]Android: package android.support.v7.app.AlertDialog does not exist
[英]Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
每次我构建我的应用程序时,我都会Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
错误
这是我的错误代码
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)
它发生在我的飞溅活动中,这是我的代码
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();
}
}
}
不知道该怎么办...请帮忙解决这个问题,
我认为它在您的checking()
方法中失败了。 您已经声明了一个名为builder
的全局变量,然后在您的onCreate()
声明了另一个变量。 在您的checking()
方法中,它指的是您未初始化、仅声明的全局变量。
可能的解决方案,编辑这个:
AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);
到
builder = new AlertDialog.Builder(Splash.this);
请使用下面的代码作为参考,它对我来说很好用。
在 onCreate() 之前
AlertDialog.Builder alertDialog;
在 OnCreate() 内
alertDialog = new AlertDialog.Builder(YourActivity.this);
创建警报对话框
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.