android findViewById在OnOptions ItemSelected中返回null
在我的布局文件夹中有一个线性布局,名为“settings\u caching\u popup.xml”,我试图在显示弹出窗口的android findViewById在OnOptions ItemSelected中返回null,android,findviewbyid,Android,Findviewbyid,在我的布局文件夹中有一个线性布局,名为“settings\u caching\u popup.xml”,我试图在显示弹出窗口的onoptions项selected(MenuItem项)方法中获取此布局。但是findViewById(R.layout.settings\u caching\u popup)总是返回null。然后,我在xml布局中使用android:ID=“@+ID/settings\u caching\u popup将一个ID设置为LinearLayout,并调用findViewB
onoptions项selected(MenuItem项)
方法中获取此布局。但是findViewById(R.layout.settings\u caching\u popup)
总是返回null。然后,我在xml布局中使用android:ID=“@+ID/settings\u caching\u popup
将一个ID设置为LinearLayout,并调用findViewById(R.ID.settings\u caching\u popup)
。返回空值
从选择的选项项(菜单项)中拉出
使用
而不是
findViewById(R.id.settings_caching_popup);
findViewById()
正在查找在调用setContentView()
(或类似内容)Use时为活动膨胀的布局中的视图
而不是
findViewById(R.id.settings_caching_popup);
findViewById()
正在查找在调用setContentView()
(或类似)时为活动膨胀的布局中的视图。findViewById()只返回已经膨胀的视图
试试这个:
final PopupWindow popUp = new PopupWindow(context);
LayoutInflater inflater = LayoutInflater.from(this);
final LinearLayout ll =
(LinearLayout)inflater.inflate(R.layout.settings_caching_popup, null);
popUp.setContentView(ll);
ll.post(new Runnable() {
public void run() {
popUp.showAtLocation(ll, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
}
});
请注意,this
在LayoutInflater.from(this);
中必须是您的活动。因此,如果您想从OnClickListener
或类似的地方调用它,您需要将YourActivity.this
放在那里。您需要放大要在弹出窗口中显示的布局。findViewById()
仅返回已膨胀的视图
试试这个:
final PopupWindow popUp = new PopupWindow(context);
LayoutInflater inflater = LayoutInflater.from(this);
final LinearLayout ll =
(LinearLayout)inflater.inflate(R.layout.settings_caching_popup, null);
popUp.setContentView(ll);
ll.post(new Runnable() {
public void run() {
popUp.showAtLocation(ll, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
}
});
注意,this
中的LayoutInflater.from(this)
必须是你的活动。因此,如果你想从一个OnClickListener
或类似的地方调用它,你需要把你的活动放在那里。你不能使用findViewById
,除非你要找的视图已经从底层xml中膨胀了。我猜你要找的视图是需要首先膨胀。视图的主要膨胀通常发生在onCreate(…)
上,其行类似于setContentView(…)
。对于菜单,膨胀也发生在onCreateOptions菜单(…)
中,您可以看到如下内容:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
// findViewById will now work for views related to the options menu
}
对于非菜单的视图,请使用布局\u充气机\u服务
,如下所示
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.settings_caching_popup, null);
View childView = findViewById(R.id.childView); // view in R.layout.settings_caching_popup
如本文其他地方所述,在充气器服务中使用findViewById
中的R.id.
和R.layout.
。在从基础xml中充气您要查找的视图之前,您不能使用findViewById
。我猜您要查找的视图需要先充气。主inf视图的膨胀通常发生在onCreate(…)
上,行如setContentView(…)
。对于菜单,膨胀也发生在onCreateOptions菜单(…)
中,您会看到以下内容:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
// findViewById will now work for views related to the options menu
}
对于非菜单的视图,请使用布局\u充气机\u服务
,如下所示
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.settings_caching_popup, null);
View childView = findViewById(R.id.childView); // view in R.layout.settings_caching_popup
如此处其他地方所述,使用findviewbyd
和R.layout.
中的R.id.
和充气机服务。您正在使用:
LinearLayout ll = (LinearLayout) findViewById(R.layout.settings_caching_popup);
该方法是通过ID查找视图,因此您应该通过ID获取它
LinearLayout ll = (LinearLayout) findViewById(R.id.settings_caching_popup);
您正在使用:
LinearLayout ll = (LinearLayout) findViewById(R.layout.settings_caching_popup);
该方法是通过ID查找视图,因此您应该通过ID获取它
LinearLayout ll = (LinearLayout) findViewById(R.id.settings_caching_popup);
方法findViewById
正在查找活动视图中的元素,方法findViewById
正在查找活动视图中的元素,我总是在onCreateOptions菜单中膨胀另一个视图:菜单本身。如果我单击其中一个项目,我想显示一个弹出窗口,其中我是的,它是在另一个没有菜单结构的xml中。是的,它是!谢谢你的提示!嗯,不能接受两个答案?!但是如果你愿意,你可以将任意多个答案标记为有用的…?我总是在OnCreateOptions菜单中膨胀另一个视图:菜单本身。如果我单击其中一个项目,我想显示另一个xml中的弹出窗口没有菜单结构。是的,是的!谢谢你的提示!嗯,不能接受两个答案?!但是如果你愿意,你可以标记任意多个有用的答案…?没有父视图,settings\u caching\u popup
是此视图的根布局听起来像是你的解决方案正在扩大该布局,正如其他答案所暗示的那样d、 没有父视图,settings\u caching\u popup
是此视图的根布局吗听起来像是您的解决方案正在膨胀该布局,正如其他答案所建议的。我测试了这两个,但都不起作用。感谢其他答案,我可以说原因:我没有膨胀视图…我测试了这两个,都不起作用。感谢其他答案,我可以说为什么:我没有夸大视图…你能解释一下,ll.post
到底是做什么的吗?打开另一个线程?这是必要的还是“只是”一种更聪明的方式?(以前没有这样做,也许我应该不时这样做。)我不知道为什么这是必要的,但是如果你不使用post()
,显示弹出窗口将失败,标记为null异常。这是因为ll.getWindowToken()
此时返回null。看起来视图的创建部分是异步完成的。使用post()
方法发布的代码在创建完成后运行,因此getWindowToken()
no more返回null。也许其他人能够更好地解释这一点……哦,post()
不会启动另一个线程。传递的runnable将在GUI线程上执行。您能解释一下,ll.post
到底做什么?打开另一个线程?这是必要的还是“只是”更聪明的方式?(以前没有这样做,也许我应该不时这样做?)我不知道为什么有必要这样做,但如果不使用post()
,显示弹出窗口将失败,并出现令牌为空的异常。这就是bec