问题描述
- android:在对话中的setView问题
- 在对话中setView如下代码:
LayoutInflater inflater = LayoutInflater.from(this);View layout = inflater.inflate(R.layout.mylayout null);TextView MyTextView = (TextView)findViewById(R.id.my_tv);MyTextView.setText(""test"");AlertDialog MyDialog;AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);MyBuilder.setTitle(""title"");MyBuilder.setView(layout);MyDialog = MyBuilder.create();MyDialog.show();
my_tv 是在 mylayout.xm中的TextView 。
问题是 MyTextView.setText(""test"");这行报出NullPointer错误。
应该怎么修改?
解决方案
你要找的控件在哪里,就到哪里去找。你把它把在这个房间里,却到另外一个房间里找,自然是找不到的:
TextView MyTextView = (TextView)layout.findViewById(R.id.my_tv);
解决方案二:
楼上回答的很对用这个代码:
TextView MyTextView = (TextView)layout.findViewById(R.id.my_tv);
时间: 2024-08-23 03:21:00