0
votes

I did this at my newsviewholder:

    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);
    }
}

The problem its when I do onClick in the cardview It throws the next error:

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)

And Ive in my AndroidManifest.xml defined the new activity, but I dont know how to solve this...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="es.laramira.atellez.moroninfo">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ActivityDetalle"></activity>
    </application>

</manifest>

Ive tried a lot of answer found in Stackoverflow and in other sites but I cant solve this...

3
You are trying to start a Fragment as Activity (which seems to be a Fragment with the name DetalleFragment.class). This can't work, the exception explains the problem. If you simply want to start an Activity with the layout of the Fragment, create an Activity and use the layout of the Fragment in this case.Kenny Seyffarth

3 Answers

1
votes

You're starting the Fragment, instead of your activity.

new Intent(view.getContext(), DetalleFragment.class);

But you can't use context.startActivity for a fragment.

If you want to start your "ActivityDetalle", as I see in your Manifest, replace the call with

new Intent(view.getContext(), ActivityDetalle.class);

Edit:

If you want to start the activity from a context, that does not belong to an activity, you have to add the intent flag Intent.FLAG_ACTIVITY_NEW_TASK

0
votes

Initialise the context in adapter either via calling a constructor like

public MyAdapter(Context c) {
        this.c=c;

}

and call the constructor in your activity when you are initialising adapter like

MyAdapter adaoter = new MyAdapter(this);

so that your Context will not be empty!

0
votes

Your Context c; is never initialised so

View v= LayoutInflater.from(c).inflate(R.layout.activity_detalle,parent,false);
            return new NewsViewHolder(v);

returns null.

Update.

As I understood you just want to assign onClickListener to an item.

Creating and setting another adapter won't solve your problem. I would say that is totally wrong way.

To add onClickListener to your card you need to edit NewsViewHolder:

public NewsViewHolder(View itemView) {
...
itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //your stuff
    }
...
});

Update.

Unfortunately, stackoverflow is a place where you can get an answer for concrete question, rather than "I insert some code, it still does not work". It is a waste of time. First of all you must learn how to read logs and debug.

Here are some resources to start with:

https://www.udacity.com/course/developing-android-apps--ud853 https://commonsware.com/Android/