片段中的警报对话框

时间:2014-04-04 18:06:26

标签: android android-fragments android-alertdialog

我已完成此代码以在片段中显示警告对话框,但是当我单击按钮时,应用程序崩溃。 谢谢你以任何方式提供帮助。

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>

代码:

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;
  }
 }

日志:

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

当我点击按钮时,应用程序崩溃了。 thnx寻求帮助

2 个答案:

答案 0 :(得分:0)

你有一个空指针异常@ RegisterActivity.onCreate(),RegisterActivity.java第33行

无论您在此处访问哪些变量,都需要在使用它们之前检查它们是否为空。

基本的调试方法是对该行上使用的每个引用/变量执行类似的操作。

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

一旦你发现什么是null,那么你就可以找到原因,一旦你知道原因,就可以解决它并从中吸取经验。

答案 1 :(得分:0)

试试这个:

假设这是你的片段:

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

}

}

希望这会有所帮助.. :)