Android ViewStub空异常膨胀

Android ViewStub空异常膨胀,android,viewstub,Android,Viewstub,我犯了一个错误 07-04 01:49:56.325: E/AndroidRuntime(9693): java.lang.NullPointerException 在事务完成后,尝试在片段中膨胀ViewStub sendMessageView = ((ViewStub) rootView .findViewById(R.id.send_message_viewstub)).inflate(); 下面是XML <ViewStub android:

我犯了一个错误

07-04 01:49:56.325: E/AndroidRuntime(9693): java.lang.NullPointerException
在事务完成后,尝试在片段中膨胀ViewStub

sendMessageView = ((ViewStub) rootView
            .findViewById(R.id.send_message_viewstub)).inflate();
下面是XML

<ViewStub
        android:id="@+id/send_message_viewstub"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:inflatedId="@+id/panel_import"
        android:layout="@layout/fragment_send_message" />

如果您总是在这个
视图存根中膨胀相同的布局,那么最好使用
包含
标记

<include layout="@layout/fragment_send_message />

同意Shawnic的观点,你给我们看的那条错误线并不能说明什么。我可以告诉你,使用片段比活动要复杂一些

但是这么说,我不认为你可以使用片段作为包含的布局? 在你的问题的上下文中,片段究竟是什么或在哪里

片段有自己的类文件,它们负责膨胀要用作片段的布局

此外,我还关注您发布的这一部分:

sendMessageView = ((ViewStub) rootView
        .findViewById(R.id.send_message_viewstub)).inflate();
现在我有了这方面的经验,但已经有一段时间了,我不确定这部分代码是否正确

Android建议您这样编写:

 ViewStub stub = (ViewStub) findViewById(R.id.stub);
 View inflated = stub.inflate();
在代码中,对象是什么类型的

sendMessageView 

您可能想先将代码更改为此,只是为了确保,我们不知道Android建议我们这样做的确切原因,但我们可能应该这样做。

请显示您的所有代码;整个logcat和你所有的消息来源。