我有以下在单击菜单项后调用的函数。基本上我有一些字符串,我尝试设置为位于AlertDialog
内的TextView。
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()
电话有关。
logcat的
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
答案 0 :(得分:8)
您需要从正确的上下文中调用findViewById()
:
TextView tvVersion = (TextView) layout.findViewById(R.id.tvAboutdialogVersion);
如果没有提供,它将从Activity上下文中调用,该上下文已加载主XML视图,并且它没有访问/引用刚刚膨胀的对话框的XML(及其TextViews)。
答案 1 :(得分:1)
试试这个:
TextView tvVersion = (TextView) alertDialog.findViewById(R.id.tvAboutdialogVersion);
而不是:
TextView tvVersion = (TextView) findViewById(R.id.tvAboutdialogVersion);