0

单击菜单项后,我会调用以下函数。基本上我有一些字符串,我尝试将其设置为位于AlertDialog.

public void createAboutDialog() {
    View layout = mContext.getSystemService(LAYOUT_INFLATER_SERVICE).inflate(R.layout.about, null);

    TextView tv = (TextView) findViewById(R.id.myTextView);
    tv.setText("My text here");

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

该问题似乎与setText()通话有关。

日志猫

08-01 14:19:33.423: D/MainActivity(1793): 1.0
08-01 14:19:33.473: D/AndroidRuntime(1793): Shutting down VM
08-01 14:19:33.473: W/dalvikvm(1793): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
08-01 14:19:33.603: E/AndroidRuntime(1793): FATAL EXCEPTION: main
08-01 14:19:33.603: E/AndroidRuntime(1793): java.lang.NullPointerException
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.example.your.chinese.name.MainActivity.createAboutDialog(MainActivity.java:105)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.example.your.chinese.name.MainActivity.onOptionsItemSelected(MainActivity.java:74)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.app.Activity.onMenuItemSelected(Activity.java:2502)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:969)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.widget.AbsListView.performItemClick(AbsListView.java:1065)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2522)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3195)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.View.dispatchTouchEvent(View.java:5564)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2052)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1813)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2058)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1827)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1879)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.View.dispatchPointerEvent(View.java:5766)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.os.Looper.loop(Looper.java:137)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at android.app.ActivityThread.main(ActivityThread.java:4441)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 14:19:33.603: E/AndroidRuntime(1793):     at dalvik.system.NativeStart.main(Native Method)
08-01 14:19:35.583: I/Process(1793): Sending signal. PID: 1793 SIG: 9
4

2 回答 2

8

您需要findViewById()从正确的上下文中调用:

TextView tvVersion = (TextView) layout.findViewById(R.id.tvAboutdialogVersion);

如果未提供,它将从 Activity 上下文中调用,该上下文已加载主 XML 视图,并且无法访问/引用您刚刚膨胀的对话框的 XML(及其 TextViews)。

于 2012-08-01T11:26:43.577 回答
1

试试这个:

TextView tvVersion = (TextView) alertDialog.findViewById(R.id.tvAboutdialogVersion);

代替:

TextView tvVersion = (TextView) findViewById(R.id.tvAboutdialogVersion);

于 2012-08-01T11:33:14.313 回答