从Fragment中启动FragmentActivity

时间:2015-02-24 14:38:21

标签: android android-fragments

我在使用以下代码时遇到问题。 当我点击图像按钮时,它会崩溃应用程序。 有人可以指出我在下面的代码出错吗?

public class training extends Fragment {
        ImageButton run;
        public training()
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState)
        {
            View rootView = inflater.inflate(R.layout.fragment_training, container, false);


            return rootView;
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            run = (ImageButton) getActivity().findViewById
                    (R.id.StartRunClass);
            run.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i= new Intent(getActivity(),Running.class);
                    getActivity().startActivity(i);
                }
            });


        }
    }

我所在的课程中的所有内容都是

public class Running extends FragmentActivity
        //implements LocationProvider.LocationCallback {
   // private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    //public LocationProvider mLocationProvider;
{
    ImageButton start, stop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_run);

获取以下运行时错误

02-24 16:12:26.377  30732-30732/project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at project.project.training.run(training.java:38)
            at project.project.training.onCreateView(training.java:30)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:694)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5392)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我很确定问题出在getActivity()返回null

覆盖OnAttach并存储对附加活动的引用:

Activity activity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = activity;
}

getActivity()替换为您的参考:

@Override
public void onClick(View v) {
    Intent i= new Intent(activity,Running.class);
    activity.startActivity(i);
}

修改

崩溃是因为run为空。我认为问题在于您使用findViewByID的方式:

run = (ImageButton) getActivity().findViewById(R.id.StartRunClass);

您应该在onCreateView并使用rootView代替getActivity执行此操作:

View rootView = inflater.inflate(R.layout.fragment_training, container, false);
run = (ImageButton) rootView.findViewById(R.id.StartRunClass);

如果它仍然崩溃,请检查fragment_training布局以确保它包含名为StartRunClass的ImageView。