public class MainActivity extends AppCompatActivity {
Button alertdfragbutton;
FragmentManager fm = getFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertdfragbutton = (Button)findViewById(R.id.button);
alertdfragbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogF alertDFragment = new AlertDialogF();
alertDFragment.show(fm, "Alert Dialog Fragment");
}
});
}
}
public class AlertDialogF extends DialogFragment {
EditText text;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setView(R.layout.fragment);
text = (EditText)getActivity().findViewById(R.id.editText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Hello, " + text.getText().toString(), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
alertDialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialog.create();
}
}
10-23 16:48:12.812 6728-6728/? I/art: Not late-enabling -Xcheck:jni (already on)
10-23 16:48:12.812 6728-6728/? I/art: Late-enabling JIT
10-23 16:48:12.814 6728-6728/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
10-23 16:48:12.853 6728-6728/com.example.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.myapplication-2/lib/x86
10-23 16:48:13.149 6728-6756/com.example.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-23 16:48:13.152 6728-6728/com.example.myapplication D/: HostConnection::get() New Host Connection established 0xa39bf090, tid 6728
10-23 16:48:13.274 6728-6756/com.example.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
10-23 16:48:13.362 6728-6756/com.example.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
10-23 16:48:13.362 6728-6756/com.example.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabfacfa0, error=EGL_SUCCESS
10-23 16:48:20.336 6728-6756/com.example.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
10-23 16:48:20.336 6728-6756/com.example.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabfbb3a0, error=EGL_SUCCESS
10-23 16:48:21.675 6728-6739/com.example.myapplication I/art: Background sticky concurrent mark sweep GC freed 24235(1101KB) AllocSpace objects, 0(0B) LOS objects, 64% free, 1303KB/3MB, paused 6.434ms total 23.341ms
10-23 16:48:22.501 6728-6728/com.example.myapplication D/AndroidRuntime: Shutting down VM
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: Process: com.example.myapplication, PID: 6728
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at com.example.myapplication.AlertDialogF$1.onClick(AlertDialogF.java:22)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-23 16:48:22.501 6728-6728/com.example.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-23 16:48:38.437 6728-6728/? I/Process: Sending signal. PID: 6728 SIG: 9
대략 이렇게 오류가 나네요..ㅠㅠ 이것저것 찾아봐도 안되길래 질문드려요
되게 쉬운 문제인데 왜이렇게 안되는걸까요ㅠㅠㅠㅠ
xml파일은 main에 버튼하나 fragment에 edittext하나 있구요