Я сделал этот код, чтобы отобразить диалоговое окно предупреждения в фрагменте, но когда я нажимаю на кнопку, приложение вылетает. спасибо за помощь в любом случае.диалоговое окно предупреждения в фрагментах
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 за помощью
вы можете вставить ошибку из журнала кошки – UDB
Моя догадкой является IntroActivity.class не объявляется в манифесте. Но мне нужно увидеть, как logcat действительно знает, что это на самом деле. – WIllJBD
Также попробуйте взять этот код и поместить его в 'onActivityCreated' вместо' onClick'. – ctzdev