简体   繁体   中英

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

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. You have declared a global variable named builder , then you declared another variable inside your onCreate() . In your checking() method, it refers to the global variable which you didn't initialize, only declare.

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()

AlertDialog.Builder alertDialog;

Within 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM