Question

I'm struggling in a little problem that i can not find! In my main activity i want to display an AlertDialog and have a checkbox if the user do not want display the dialog again.

I can make the dialog appear with the checkbox, etc.. but for some reason when I try to add a OnCheckedChangeListener to the CheckBox my application closes before it starts :(

Here is the relevant parts of my code:

Initializing the variables for the SharedPreferences:

public final static String WEL_PREF = "Welcome Dialog Preference";
public static int welcomeChecked = 0;

Inside OnCreate:

SharedPreferences welcomePref = getSharedPreferences(WEL_PREF, MODE_PRIVATE);
welcomeChecked = welcomePref.getInt("tut", 0);

... 


    if(welcomeChecked == 1) {  

    showAlertDialog(); 

    }

the showAlertDialog() function:

  private void showAlertDialog() {

    final Intent tutorial = new Intent(MainActivity.this, TutorialActivity.class);  
    View checkBoxView = View.inflate(this, R.layout.welcome_checkbox, null);    
    CheckBox welcomeCheck = (CheckBox) findViewById(R.id.tutCheck);         
    welcomeCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            SharedPreferences welcomePref = getSharedPreferences(WEL_PREF, MODE_PRIVATE);
            SharedPreferences.Editor editor = welcomePref.edit();

            if(isChecked) {
            editor.putInt("tut", 1);
            editor.commit();
            } else { 
                 editor.putInt("tut", 0);
                 editor.commit();
            }

        }
    });

    AlertDialog.Builder welcomeDialog = new AlertDialog.Builder(this);
    welcomeDialog.setTitle("Welcome");      
    welcomeDialog.setMessage("Welcome to Formula Calculator, if you want to know how to use my features " +
            "click in the Tutorial button.");
    welcomeDialog.setView(checkBoxView);
    welcomeDialog.setCancelable(false);
    welcomeDialog.setPositiveButton("Tutorial", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            startActivity(tutorial);
        }
    });

    welcomeDialog.setNegativeButton("Close", new DialogInterface.OnClickListener() {            
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();                
        }
    });              
    AlertDialog welDialog = welcomeDialog.create();                 
    welDialog.show();
}

Thank you very much for your help! I can not find the solution why the application closes.!

Here is the logcat if helps:

05-19 15:37:55.807: W/ActivityThread(8009): Application com.gabilheri.marcuscalculatorv2 can be 

debugged on port 8100...
05-19 15:37:56.127: D/AndroidRuntime(8009): Shutting down VM
05-19 15:37:56.127: W/dalvikvm(8009): threadid=1: thread exiting with uncaught exception (group=0x40c32930)
05-19 15:37:56.137: E/AndroidRuntime(8009): FATAL EXCEPTION: main
05-19 15:37:56.137: E/AndroidRuntime(8009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabilheri.marcuscalculatorv2/com.gabilheri.marcuscalculatorv2.MainActivity}: java.lang.NullPointerException
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.os.Looper.loop(Looper.java:137)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at java.lang.reflect.Method.invoke(Method.java:511)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at dalvik.system.NativeStart.main(Native Method)
05-19 15:37:56.137: E/AndroidRuntime(8009): Caused by: java.lang.NullPointerException
05-19 15:37:56.137: E/AndroidRuntime(8009):     at com.gabilheri.marcuscalculatorv2.MainActivity.showAlertDialog(MainActivity.java:744)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at com.gabilheri.marcuscalculatorv2.MainActivity.onCreate(MainActivity.java:171)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.Activity.performCreate(Activity.java:5104)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-19 15:37:56.137: E/AndroidRuntime(8009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-19 15:37:56.137: E/AndroidRuntime(8009):     ... 11 more
05-19 15:37:56.187: D/dalvikvm(8009): GC_CONCURRENT freed 219K, 5% free 7518K/7864K, paused 14ms+2ms, total 80ms
Was it helpful?

Solution

if tutCheck CheckBox is in Dialog Layout then initialize it as:

View checkBoxView = View.inflate(this, R.layout.welcome_checkbox, null); 
CheckBox welcomeCheck = (CheckBox)checkBoxView.findViewById(R.id.tutCheck); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top