Fragment类在intent的startactivity方法中显示错误

时间:2016-08-30 06:31:46

标签: java android

  

-30 12:14:06.020 24369-24369 / com.sourcey.materialloginexample E / AndroidRuntime:FATAL EXCEPTION:main                                                                                     处理:com.sourcey.materialloginexample,PID:24369                                                                                     android.content.ActivityNotFoundException:无法找到显式   活动课   {com.sourcey.materialloginexample / com.surbhi.oopscourier.activities.TwoFragment};   你有没有在AndroidManifest.xml中声明这个活动?                                                                                         在   android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1788)                                                                                         在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)                                                                                         在android.app.Activity.startActivityForResult(Activity.java:3810)                                                                                         在android.app.Activity.startActivityForResult(Activity.java:3761)                                                                                         在   android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:813)                                                                                         在   android.support.v4.app.FragmentActivity $ HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:871)                                                                                         在android.support.v4.app.Fragment.startActivity(Fragment.java:916)                                                                                         在   com.surbhi.oopscourier.activities.OneFragment $ 1.onClick(OneFragment.java:26)                                                                                         在android.view.View.performClick(View.java:4848)                                                                                         在android.view.View $ PerformClick.run(View.java:20262)                                                                                         在android.os.Handler.handleCallback(Handler.java:815)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:104)                                                                                         在android.os.Looper.loop(Looper.java:194)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5637)                                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                                         在java.lang.reflect.Method.invoke(Method.java:372)                                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

public class OneFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one, container, false);
            Button next=(Button)view.findViewById(R.id.nextonefrag);
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i=new Intent(getActivity(),TwoFragment.class);
                    startActivity(i);
                }
            });
            return view;
        }
    }

xml:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/cell"
        android:layout_marginTop="150dp"
        android:orientation="vertical">


        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:background="@color/colorPrimary"
            android:id="@+id/spinner" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wait and Return"
            android:textColor="#000000"
            android:id="@+id/checkBox" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Express"
            android:textColor="#000000"
            android:id="@+id/checkBox2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:layout_marginBottom="10dp"
            android:background="@color/colorPrimary"
            android:id="@+id/nextonefrag"
            android:layout_marginRight="20dp"
            android:layout_gravity="right" />
    </LinearLayout>
</RelativeLayout>

当我点击片段内的按钮时,应用程序不幸停止了。并且在启动Activity方法中显示错误,指示在清单文件中声明片段活动。我试图在清单中声明片段活动但是它不能在那里声明。< / p>

3 个答案:

答案 0 :(得分:0)

如果您的TwoFragment.java扩展了Fragment并且您已动态添加了片段,则可以将此前片段替换为Fragment TwoFragment,如下所示:

Fragment fragment = new TwoFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame, fragment);
        fragmentTransaction.commit();

活动和片段是两回事:

活动是一个提供屏幕的应用程序组件,用户可以通过该屏幕进行交互以执行某些操作。更多详情:http://developer.android.com/guide/components/activities.html

而Fragment表示Activity中的行为或用户界面的一部分。您可以在单个活动中组合多个片段以构建多窗格UI,并在多个活动中重用片段。您可以将片段视为活动的模块化部分,它具有自己的生命周期,接收自己的输入事件,并且您可以在活动运行时添加或删除(有点像&#34;子活动&# 34;您可以在不同的活动中重复使用)。片段必须始终嵌入到活动中,片段的生命周期直接受主机活动生命周期的影响。 http://developer.android.com/guide/components/fragments.html

答案 1 :(得分:0)

如果您需要将第一个片段替换为第二个片段,请单击“使用片段事务处理”。程序发生的错误是因为您正在尝试加载像活动一样的片段。所以用Fragment Transaction替换代码。

答案 2 :(得分:0)

是一个活动或片段的两个片段?

1.只能在Manifest中声明活动。 2.如果TwoFragment是一个片段,你需要一种不同的方法来切换片段..