2013-06-12 63 views
-1

我的布局文件夹中有一个LinearLayout,名为“settings_caching_popup.xml”我试图在onOptionsItemSelected(MenuItem item)方法中显示弹出窗口的布局。但findViewById(R.layout.settings_caching_popup)返回始终为空。然后我将xml Layout中的ID设置为LinearLayout,其中android:id="@+id/settings_caching_popupfindViewById(R.id.settings_caching_popup)。返回值也为null。android findViewById在onoptionsitemselected中返回null

拔出了onOptionsItemSelected(MenuItem item)的:

PopupWindow popUp = new PopupWindow(context); 

LinearLayout ll = (LinearLayout) findViewById(R.layout.settings_caching_popup); 

popUp.setContentView(ll); 
popUp.showAtLocation(ll, Gravity.BOTTOM, 10, 10); 
popUp.update(50, 50, 300, 80); 
+0

方法'findViewById'正在寻找一个元素在活动的视图内, – bogdan

回答

2

您需要膨胀要在弹出窗口中显示的布局。 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); 
    } 
}); 

通知书的,thisLayoutInflater.from(this);必须是你的活动。所以,如果你想从OnClickListener或类似的东西打电话给你,你需要把YourActivity.this放在那里。

+0

是的,它工作正常!谢谢! – lis

+0

你能解释一下吗,ll.post究竟是干什么的?打开另一个线程?这是必要的还是“仅仅”一个更聪明的方法? (之前没有这样做,也许我应该不时地这样做) – lis

+1

我不知道为什么这是必要的,但是如果你不使用'post()',显示弹出窗口将会失败一个标记是空的异常。那是因为会。getWindowToken()此时返回null。看起来,像创建视图是部分完成异步。使用'post()'方法发布的代码在创建完成后运行,这样'getWindowToken()'不会再返回null。也许别人能够更好地解释这个...... –

0

使用

[parent view of settings_caching_popup].findViewById(R.id.settings_caching_popup); 

,而不是

findViewById(R.id.settings_caching_popup); 

findViewById()正在寻找布局内的意见认为,在填充的活动时,你打电话setContentView()(或类似的东西)

+0

Theres没有父视图,'settings_caching_popup'是这个视图的根布局 – lis

+0

听起来像你的解决方案正在膨胀那个布局,然后,如其他答案已经建议。 – invertigo

1

您不能使用findViewById,直到您正在查找的视图从基础xml中被夸大为止。我的猜测是,你正在寻找的视图需要首先被夸大。观点的主要膨胀通常发生在onCreate(...),其线条如setContentView(...)。对于菜单,通胀也出现在onCreateOptionsMenu(...)在那里你看到喜欢的东西:

@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 
} 

对于视图不属于菜单,使用LAYOUT_INFLATER_SERVICE其他地方一样这里如下

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.与inflater服务。

+0

我总是在onCreateOptionsMenu中添加另一个视图:菜单本身。如果我点击这些项目中的一个,我想显示一个弹出窗口,在另一个xml中没有菜单结构。 – lis

+0

我已经更新了我的答案,这是否工作? – Stochastically

+0

是的,它的确如此!谢谢你的提示!嗯,不能接受两个答案?! – lis

1

您使用:

LinearLayout ll = (LinearLayout) findViewById(R.layout.settings_caching_popup); 

的方法查找ID查看,所以你应该得到它通过它的ID

LinearLayout ll = (LinearLayout) findViewById(R.id.settings_caching_popup); 
+0

我测试了两个,没有工作。由于其他答案,我可以说为什么:我没有夸大视图... – lis