从片段到活动的事务(java类)

时间:2014-05-26 23:28:51

标签: android android-fragments android-activity fragment

  

FragmentOne

public class FragmentOne extends Fragment {




    public FragmentOne() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_main2, container,
                true);


        return view;
    }
}

您好,我是一名初学者,我尝试了所有我能找到的,问题是我需要这个片段初始化类BaseAct.java,在片段引用activity_main2之后,我发现任何东西,所以我希望你能帮助我,谢谢你的建议

如果您需要,我可以链接所有源文件

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Intent intent = new Intent(getActivity(), BaseAct.class);
    startActivity(intent);

    View view = inflater.inflate(R.layout.activity_main2, container,
            false);


    return view;
}
  

logcat的

Process: com.elitiv.myapplication2.app, PID: 2941
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elitiv.myapplication2.app/com.elitiv.myapplication2.app.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.elitiv.myapplication2.app/com.elitiv.myapplication2.app.BaseAct}; have you declared this activity in your AndroidManifest.xml?
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.elitiv.myapplication2.app/com.elitiv.myapplication2.app.BaseAct}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)
            at android.app.Activity.startActivityFromFragment(Activity.java:3957)
            at android.app.Activity.startActivityFromFragment(Activity.java:3932)
            at android.app.Fragment.startActivity(Fragment.java:1054)
            at android.app.Fragment.startActivity(Fragment.java:1033)
            at com.elitiv.myapplication2.app.FragmentOne.onCreateView(FragmentOne.java:23)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
            at android.app.Activity.performStart(Activity.java:5240)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您可以通过向您要启动的Activity发送明确的Fragment,从onCreateView()的{​​{1}}开始新的Intent

此外,与rcoro说的一样,请将Activity传递给false的第3个参数。传递inflate()将阻止框架将false添加到层次结构两次,这将导致错误。

答案 1 :(得分:0)

你好用false替换true:     查看view = inflater.inflate(R.layout.activity_main2,container,false);