Wednesday, March 9, 2011

[android-developers] Honeycomb / Fragments: can't remove a fragment added in XML layout

Hi,

I can't remove a Fragment that was added in an XML layout file, using
a FragmentTransaction. I don't understand why, I probably
misunderstood something and I would appreciate some explanations.

----
Here's a simple code to demonstrate my problem :

** Main layout, main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/root"
>
<TextView android:id="@+id/hello_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

<fragment android:name="test.fragments.FragmentA"
android:id="@+id/fragment_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

** Main activity:
public class TestFragmentsActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView helloView = (TextView)
findViewById(R.id.hello_view);
helloView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
FragmentManager fragmentManager =
getFragmentManager();
FragmentTransaction transaction =
fragmentManager.beginTransaction();

transaction.remove(fragmentManager.findFragmentById(R.id.fragment_a));
transaction.commit();
}
});
}
}

** My fragment class:
public class FragmentA extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_a, container,
false);
}
}

** Fragment layout, layout_a.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="TextView A" android:id="@+id/
textView1_a" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
----

When I click on the text view, the fragment should be removed, but
nothing seems to happen. If I click again, the application crashes
with the following error messages:

03-09 09:48:09.248: ERROR/AndroidRuntime(1222):
java.lang.NullPointerException
03-09 09:48:09.248: ERROR/AndroidRuntime(1222): at
android.app.BackStackRecord.remove(BackStackRecord.java:365)
03-09 09:48:09.248: ERROR/AndroidRuntime(1222): at
test.fragments.TestFragmentsActivity
$1.onClick(TestFragmentsActivity.java:28)


Maybe it's not possible to remove fragments that were added using XML
layouts?

Thanks in advance.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment