I have an Activity which hosts a Fragment.
The Activity layout file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.my.ContentFragment"
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Java code of Activity:
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
public class ContentActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//data from previous Activity
Bundle data = getIntent().getExtras();
Fragment contentFragment = getSupportFragmentManager()
.findFragmentById(R.id.fragment_content);
//Pass data to fragment
/*NullpointerException, contentFragment is null!*/
contentFragment.setArguments(data);
setContentView(R.layout.activity_main);
}
...
}
I try to find the fragment in onCreate()
of Activity, and then pass some data to it. But when I findFragmentById(...)
, I got NullpointerException, Why?