堆栈跟踪:
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: FATAL EXCEPTION: main
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: Process: eu.niehus.testapp, PID: 31756
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: java.lang.NullPointerException
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at eu.niehus.testapp.DialogTools.createDialog(DialogTools.java:13)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at eu.niehus.testapp.Dashboard.onOptionsItemSelected(Dashboard.java:49)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.app.Activity.onMenuItemSelected(Activity.java:2600)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:325)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:73)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:180)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:761)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.widget.AdapterView.performItemClick(AdapterView.java:299)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.widget.AbsListView$3.run(AbsListView.java:3645)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5214)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:814)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:630)
10-14 20:56:45.609 31756-31756/eu.niehus.testapp E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我的代码:
public class DialogTools extends DialogFragment
{
public void createDialog(String title, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(message);
builder.setTitle(title);
AlertDialog dialog = builder.create();
}
}
如果我调用方法&#34; createDialog&#34;从我的主要活动中我得到一个java.lang.NullPointerException。是不是可以将AlertDialog Builder放在一个单独的类中?
答案 0 :(得分:7)
你得到一个NPE因为对话框片段没有经过它的生命周期而getActivity()
返回null。覆盖
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
返回AlertDialog
,并在DialogFragment
的实例上调用show()