3

I get an Error

07-04 01:49:56.325: E/AndroidRuntime(9693): java.lang.NullPointerException

while try to inflate ViewStub in Fragment after transaction done

sendMessageView = ((ViewStub) rootView
            .findViewById(R.id.send_message_viewstub)).inflate();

Here are 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" />
Ramy Sabry
  • 378
  • 2
  • 9

2 Answers2

-1

Agree with Shawnic, that error line you are showing us doesn't tell much. I can tell you that using fragments is a little trickier than activities.

But with that said, I don't think you can use a fragment as the included layout? What or where exactly is the Fragment within the context of your question?

Fragments have their own class files that take care of inflating a layout to be used as the Fragment.

Also I am concerned about this part you posted:

sendMessageView = ((ViewStub) rootView
        .findViewById(R.id.send_message_viewstub)).inflate();

Now I have experience with this but it's been a while and I am not sure if this part of your code is correct?

This is how Android recommends you write it:

 ViewStub stub = (ViewStub) findViewById(R.id.stub);
 View inflated = stub.inflate();

In Your Code, what kind of object is

sendMessageView 

You may want to change your code to this first just to make sure, we don't know exactly why Android suggests we do it this way but we probably should.

-2

If you're always going to be inflating the same layout in this ViewStub, you would be better served using an include tag.

<include layout="@layout/fragment_send_message />

include replaces itself with the specified layout at runtime and allows your layouts to be both modular and readable.

peteross
  • 236
  • 3
  • 12