MainActivity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//First fragment should be mounted on oncreate of main activity
if (savedInstanceState == null) {
Fragment newFragment = FragmentOne.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment).addToBackStack(null).commit();
}
}
}
FragmentOne.java
public class FragmentOne extends Fragment{
Button btn;
public static FragmentOne newInstance(){
FragmentOne fragment = new FragmentOne();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
btn=(Button) getActivity().findViewById(R.id.button1);
return view;
}
@Override
public void onStart() {
super.onStart();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getActivity().getSupportFragmentManager();
Fragment frgObj=FragmentOne.newInstance();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.container, frgObj,"FragmentOne");
ft.addToBackStack(null);
ft.commit();
}
});
}
}
activity_main.xml中
<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:orientation="vertical"
android:id="@+id/container">
</LinearLayout>
的日志::
08-09 15:00:50.327: D/AndroidRuntime(769): Shutting down VM
08-09 15:00:50.327: W/dalvikvm(769): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-09 15:00:50.357: E/AndroidRuntime(769): FATAL EXCEPTION: main
08-09 15:00:50.357: E/AndroidRuntime(769): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stylingandroid/com.example.stylingandroid.MainActivity}: java.lang.NullPointerException
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.os.Looper.loop(Looper.java:137)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-09 15:00:50.357: E/AndroidRuntime(769): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 15:00:50.357: E/AndroidRuntime(769): at java.lang.reflect.Method.invoke(Method.java:511)
08-09 15:00:50.357: E/AndroidRuntime(769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-09 15:00:50.357: E/AndroidRuntime(769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-09 15:00:50.357: E/AndroidRuntime(769): at dalvik.system.NativeStart.main(Native Method)
08-09 15:00:50.357: E/AndroidRuntime(769): Caused by: java.lang.NullPointerException
08-09 15:00:50.357: E/AndroidRuntime(769): at com.example.stylingandroid.FragmentOne.onStart(FragmentOne.java:37)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.support.v4.app.Fragment.performStart(Fragment.java:1524)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:968)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1900)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:587)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.Activity.performStart(Activity.java:4475)
08-09 15:00:50.357: E/AndroidRuntime(769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
08-09 15:00:50.357: E/AndroidRuntime(769): ... 11 more
答案 0 :(得分:1)
您的Fragment
View
未附加到Activity
onCreateView()
方法,因此btn
为null
改变这一行
btn=(Button) getActivity().findViewById(R.id.button1);
进入
btn=(Button)view.findViewById(R.id.button1);
否则将该行移至onStart()
方法