尝试调用虚方法' android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog $ Builder.create()'在null对象引用上

时间:2016-07-26 08:34:49

标签: java android android-studio android-alertdialog

每次构建应用时都出现错误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();
        }
    }
}

它发生在我的启动活动上,这是我的代码

CloudBlockBlob.OpenRead()
不知道该做什么......请帮助解决这个问题,

2 个答案:

答案 0 :(得分:0)

我认为它在checking()方法失败了。您已声明了一个名为builder的全局变量,然后在onCreate()内声明了另一个变量。在checking()方法中,它指的是您没有初始化的全局变量,只是声明。

可能的解决方案,编辑:

AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);

builder = new AlertDialog.Builder(Splash.this);

答案 1 :(得分:0)

请使用下面的代码作为参考,对我来说很好。

在onCreate()之前

AlertDialog.Builder alertDialog;

在OnCreate()之内

alertDialog = new AlertDialog.Builder(YourActivity.this);

创建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();