我想打开一个选定的cardview的详细信息,而我试图采用onlcick并打开一个新的意图,但是Android Studio抛出下一个异常:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: es.laramira.atellez.moroninfo, PID: 5200
android.content.ActivityNotFoundException: Unable to find explicit activity class {es.laramira.atellez.moroninfo/es.laramira.atellez.moroninfo.Fragments.DetalleFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at es.laramira.atellez.moroninfo.Models.NewsViewHolder$1.onClick(NewsViewHolder.java:32)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


这是我的ViewHolder的代码:

public class NewsViewHolder extends RecyclerView.ViewHolder {

    View mView;
    Context context;

    public NewsViewHolder(final View itemView) {
        super(itemView);
        mView = itemView;
        itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), DetalleFragment.class);
                view.getContext().startActivity(intent);
            }
        });
    }

    public void setTitular(String titular) {
        TextView post_titular = (TextView) mView.findViewById(R.id.titleText);
        post_titular.setText(titular);
    }

    public void setImage(Context ctx, String image){
        ImageView post_image = (ImageView) mView.findViewById(R.id.imageView);
        Picasso.with(ctx).load(image).into(post_image);
    }
}


这是我的DetalleFragment.java

public class DetalleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detalle, container, false);
    }
}


这是我的fragment_detalle.xml

<FrameLayout 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"
    tools:context="es.laramira.atellez.moroninfo.Fragments.DetalleFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello from detail"
        android:textSize="24dp"/>

</FrameLayout>

最佳答案

DetalleFragment是一个片段。您需要活动类来增加片段。
您不能直接为DetalleFragment调用startActivity()。

创建一个活动类并为DetalleFragment充气,然后为该活动类调用startActivity()。

10-01 09:05