我每次構建我的應用程序時遇到了錯誤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();
}
}
}
不知道該怎麼辦...請幫助解決這個問題,
發佈一些代碼,請爲您的閃屏:) –
不,我的意思是,只需複製你的代碼在你的閃屏,編輯你的問題,並將代碼粘貼到你的問題,這樣人們可以看到你的代碼,並幫助你:) –
哦我的..我試過..但總是說錯誤代碼..並繼續嘗試現在 –