【问题标题】:NPE when inflating layout膨胀布局时的 NPE
【发布时间】:2013-10-31 14:47:43
【问题描述】:

我必须在其他布局的子布局(线性布局)中设置布局。为此,我在要设置到根布局中的布局活动上编写以下代码:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    **setContentView(R.layout.main);**

    /**Define root layout's child where I want to set the layout*/
    LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);

    /**Inflate this layout and add it to the root layout*/
    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View this_layout = inflater.inflate(R.layout.main, null);
    inside_menu_view.addView(this_layout);

但我在最后一行 inside_menu_view.addView(this_layout); 得到一个 NULLPOINTEREXCEPTION

更新 - 在 super.onCreate 之后添加 setContentView()

【问题讨论】:

  • 在访问activitycontent 布局之前,您在哪里调用setContentView
  • @ρяσѕρєя K 我在 super.onCreate 之后调用它...我更新了帖子
  • @codeMagic 否,activitycontent 是其他布局的子布局,我想在其中插入 main 布局
  • 发布。堆。痕迹。和布局主要。
  • @njzk2 抱歉,我不明白你的意思

标签: android android-layout inflate


【解决方案1】:

这行将返回null,因为你还没有调用setContentView()

 LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);

您需要先致电setContentView(R.layout.layout_with_activitycontent);

From the Docs

从 onCreate(Bundle) 中处理的 XML 中查找由 id 属性标识的视图。

如果找到则返回视图,否则返回 null。

您尚未使用 setContentView() 或使用 LayoutInflater 处理 xml layout 文件,因此当您尝试调用 addView() 之类的方法时,它会 return null 导致 NPE

编辑

我不确定你为什么要这样做,但你不能。如我上面的链接,您只能使用findViewById() 在膨胀的layout 中找到View。您不能使用它在其他尚未膨胀的layout 中找到View

如果您想使用那个 layout 文件,请将其放入 setContentView()

不同的方法 您可能想使用Fragments 来实现这一点。

See the docs on how to use these

或者您可以使用<include> 来包含您想要包含在所有Activities 中的“菜单”layout,然后您可以在需要显示@987654347 的内部部分时切换Activities @。

See here about re-using layouts

<include> 的示例。你有一些layout 想要重复使用,比如main.xml,然后在Activity 中你想要重复使用它,你只需执行类似

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="30dp"
    android:background="@drawable/blue">
        <include layout="@layout/main"
        android:id="@+id/main_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        <!-- other layouts -->
/<RelativeLayout>

你的显然会有所不同,但这是我的一个例子。希望这会有所帮助。

【讨论】:

  • 用我添加 setContentView() 的地方更新了帖子...但在同一个地方仍然有相同的 NPE
  • 您是否刚刚将其添加到您的代码中?如果是这样,请尝试清理您的项目(“Project --> Clean...”)并再次运行它。另外,请确保activitycontentLinearLayout 内部LinearLayoutid main.xml
  • 正如我在上面的答案中回答你的那样,activitycontent 属于其他不同的布局
  • 是的,在看到您的评论之前我已经发表了评论。无论如何,请参阅我的编辑,请不要更改您的 OP 中的代码,这会让其他人感到困惑。
  • 看看这个,这里我解释一下我需要实现什么,可能你会看得更清楚stackoverflow.com/questions/19705485/…
【解决方案2】:

假设你的 R.layout.main 是一个 LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_layout" >
</LinearLayout>

现在在 onCreate() 中:

LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View this_layout = inflater.inflate(R.layout.main_layout, layout, true);

并且 this_layout 会自动添加到 Activity 的布局中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    相关资源
    最近更新 更多