2014-11-23 41 views
-1

我有大问题inflate累了。android充气视图findViewById不工作

我在我的应用程序的另一个布局,这是message

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <TextView 
      android:layout_width="match_parent" 
      android:layout_height="369dp" 
      android:text="New Text" 
      android:id="@+id/msg"/> 
    <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button"/> 
    <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button2"/> 
</LinearLayout> 

,当我试图改变msg文本它的失败:

View vw = getLayoutInflater().inflate(R.layout.message, null); 

msg = (TextView) vw.findViewById(R.id.msg); 

msg.setText("123"); 

setContentView(R.layout.message); 

和应用程序也不例外。

我该如何解决它?

回答

1

必须setContentView通过与新景观:

setContentView(vw); 
+0

好极了,还有Tnx。 – 2014-11-23 08:43:29

1

如果它的片段,您必须返回大众汽车作为您的视图。

Return vw; 

如果其活动必须将vw设置为内容视图。

setContentView(vw); 
+0

哪里是片段? – 2014-11-23 08:44:17

+0

片段是一个不同的类,取决于你的班级。你在使用活动还是片段? – SacreDeveloper 2014-11-23 08:44:47

+0

活动如何使用片段? – 2014-11-23 08:46:34