Question

i have done this code to show alert dialog in a fragment but when i click on the button , the application crashes. thanks for helping in any way.

fragment_disconnect.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal"
  android:background="#EBB917" >

  <Button
     android:layout_margin="20dip"
     android:id="@+id/button_disconnect_alert"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Disconnect" 
     android:onClick="onClick" />

</LinearLayout>

code:

package com.example.poca2;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DisconnectFragment extends Fragment {
public DisconnectFragment(){}


public void onClick(View view)
{
    AlertDialog.Builder alertDialogBuilder = new  AlertDialog.Builder(getActivity());
    // set Title
    alertDialogBuilder.setTitle("Disconnecting User");
    //set Message
    alertDialogBuilder.setMessage("Are You Sure You Want To Disconnect ? ").setCancelable(false).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id){
            // if button is clicked ,then go to into activity
            //Intent intent = new Intent(getActivity(), IntroActivity.class);
            //startActivity(intent);
        }
    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id ){
            // if button is clicked then close the alert box

            dialog.cancel();
        }
    });
    //create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    //show alert box
    alertDialog.show();
}       




@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_disconnect, container,  false);
    return rootView;
  }
 }

log:

04-03 11:37:22.684: D/skia(23439): new locale en-Latn-GB
04-03 11:37:22.854: D/libEGL(23439): loaded /vendor/lib/egl/libEGL_adreno.so
04-03 11:37:22.854: D/libEGL(23439): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-03 11:37:22.864: D/libEGL(23439): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-03 11:37:22.864: I/Adreno-EGL(23439): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
04-03 11:37:22.864: I/Adreno-EGL(23439): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-03 11:37:22.864: I/Adreno-EGL(23439): Build Date: 09/26/13 Thu
04-03 11:37:22.864: I/Adreno-EGL(23439): Local Branch: 
04-03 11:37:22.864: I/Adreno-EGL(23439): Remote Branch: 
04-03 11:37:22.864: I/Adreno-EGL(23439): Local Patches: 
04-03 11:37:22.864: I/Adreno-EGL(23439): Reconstruct Branch: 
04-03 11:37:22.914: D/OpenGLRenderer(23439): Enabling debug mode 0
04-03 11:37:39.971: D/AndroidRuntime(23439): Shutting down VM
04-03 11:37:39.971: W/dalvikvm(23439): threadid=1: thread exiting with uncaught exception (group=0x41b2b898)
04-03 11:37:39.981: E/AndroidRuntime(23439): FATAL EXCEPTION: main
04-03 11:37:39.981: E/AndroidRuntime(23439): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.poca2/com.example.poca2.RegisterActivity}: java.lang.NullPointerException
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.os.Looper.loop(Looper.java:137)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at java.lang.reflect.Method.invoke(Method.java:525)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at dalvik.system.NativeStart.main(Native Method)
04-03 11:37:39.981: E/AndroidRuntime(23439): Caused by: java.lang.NullPointerException
04-03 11:37:39.981: E/AndroidRuntime(23439):    at com.example.poca2.RegisterActivity.onCreate(RegisterActivity.java:33)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.Activity.performCreate(Activity.java:5372)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-03 11:37:39.981: E/AndroidRuntime(23439):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-03 11:37:39.981: E/AndroidRuntime(23439):    ... 11 more
04-03 11:37:44.545: D/skia(23761): new locale en-Latn-GB
04-03 11:37:44.776: D/libEGL(23761): loaded /vendor/lib/egl/libEGL_adreno.so
04-03 11:37:44.786: D/libEGL(23761): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-03 11:37:44.786: D/libEGL(23761): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-03 11:37:44.786: I/Adreno-EGL(23761): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
04-03 11:37:44.786: I/Adreno-EGL(23761): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-03 11:37:44.786: I/Adreno-EGL(23761): Build Date: 09/26/13 Thu
04-03 11:37:44.786: I/Adreno-EGL(23761): Local Branch: 
04-03 11:37:44.786: I/Adreno-EGL(23761): Remote Branch: 
04-03 11:37:44.786: I/Adreno-EGL(23761): Local Patches: 
04-03 11:37:44.786: I/Adreno-EGL(23761): Reconstruct Branch: 
04-03 11:37:44.836: D/OpenGLRenderer(23761): Enabling debug mode 0
04-03 11:37:48.069: D/AndroidRuntime(23761): Shutting down VM
04-03 11:37:48.069: W/dalvikvm(23761): threadid=1: thread exiting with uncaught exception (group=0x41b2b898)
04-03 11:37:48.069: E/AndroidRuntime(23761): FATAL EXCEPTION: main
04-03 11:37:48.069: E/AndroidRuntime(23761): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.poca2/com.example.poca2.RegisterActivity}: java.lang.NullPointerException
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.os.Looper.loop(Looper.java:137)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at java.lang.reflect.Method.invoke(Method.java:525)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at dalvik.system.NativeStart.main(Native Method)
04-03 11:37:48.069: E/AndroidRuntime(23761): Caused by: java.lang.NullPointerException
04-03 11:37:48.069: E/AndroidRuntime(23761):    at com.example.poca2.RegisterActivity.onCreate(RegisterActivity.java:33)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.Activity.performCreate(Activity.java:5372)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-03 11:37:48.069: E/AndroidRuntime(23761):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-03 11:37:48.069: E/AndroidRuntime(23761):    ... 11 more
04-03 11:37:54.265: I/Process(23761): Sending signal. PID: 23761 SIG: 9

the application crashes on when i click on the button. thnx for help

Was it helpful?

Solution

Try this :

Assume this is your fragment :

public class SomeFragment extends Fragment {

    public SomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_main, container,
                false);

        return rootView;
    }

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    Button btn = (Button) getActivity().findViewById(
            button_disconnect_alert);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getActivity());
            builder.setIcon(getResources().getDrawable(
                    R.drawable.ic_launcher));
            builder.setTitle("Some title");
            builder.setMessage("Some Message");
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

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

                            Toast.makeText(getActivity(),
                                    "Positive clicked", Toast.LENGTH_SHORT)
                                    .show();

                            Intent intent = new Intent(getActivity(),
                                    IntroActivity.class);
                            getActivity().startActivity(intent);

                        }
                    });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

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

                            Toast.makeText(getActivity(),
                                    "Negative clicked", Toast.LENGTH_SHORT)
                                    .show();

                        }
                    });
            builder.setCancelable(false);
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

}

}

Hope this helps .. :)

OTHER TIPS

You have a null pointer exception @ RegisterActivity.onCreate(), RegisterActivity.java line 33

Whatever variables you are accessing here, you need to check if they are null before using them.

The basic debug method would be to do something like this for every reference/variable used on that line.

if (variable == null)
{
    Log.d(TAG, "variable is null")
}

once you find out what is null, then you can find out why, and once you know why, you can fix it and learn from the experience.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top