Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android.support.v7.widget.TintContextWrapper cannot be cast

Tags:

android

I am making a task reminder app following some tutorials. It has two activities namely TaskListActivity and TaskEditActivity. TaskList activity is running fine but when I try to navigate to TaskEditActivity using intent ,it crashes. Below is my manifest file:

<?xml version="1.0" encoding="utf-8"?>

<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=".TaskListActivity"
        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=".TaskEditActivity"
        android:label="@string/title_activity_task_edit"
        android:parentActivityName=".TaskListActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.dummies.tasks.activity.TaskListActivity" />
    </activity>
</application>

And below is my build.gradle file:

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.dummies.tasks"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'

    compile 'com.squareup.picasso:picasso:2.4.0'

}

It's Logcat is as follows:

FATAL EXCEPTION: main
                                                                   Process: com.dummies.tasks, PID: 10754
                                                                   **java.lang.ClassCastException: android.support.v7.widget.TintContextWrapper cannot be cast to com.dummies.tasks.interfaces.OnEditTask**
                                                                       at **com.dummies.tasks.adapter.TaskListAdapter$1.onClick(TaskListAdapter.java:51)
                                                                       at android.view.View.performClick(View.java:4791)**
                                                                       at android.view.View$PerformClick.run(View.java:19884)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
08-07 15:16:46.625 10754-10754/com.dummies.tasks I/Process: Sending signal. PID: 10754 SIG: 9

Adapter class :

package com.dummies.tasks.adapter;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dummies.tasks.activity.R;
import com.dummies.tasks.interfaces.OnEditTask;
import com.squareup.picasso.Picasso;

/**
 * Created by panwa on 8/7/2016.
 */
public class TaskListAdapter
        extends RecyclerView.Adapter<TaskListAdapter.ViewHolder>
        {
static String[] fakeData = new String[] {
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Ah . . . ah . . . ah!"
        };
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
// create a new view
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
        .inflate(R.layout.card_task, parent, false);
// wrap it in a ViewHolder
        return new ViewHolder(v);
        }
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
    final Context context = viewHolder.titleView.getContext();
    viewHolder.titleView.setText(fakeData[i]);
// set the thumbnail image
    Picasso.with(context)
            .load(getImageUrlForTask(i))
            .into(viewHolder.imageView);
// Set the click action
    viewHolder.cardView.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((OnEditTask) context).editTask(i);
                }
            });
}
@Override
public int getItemCount() {
        return fakeData.length;
        }
            public static String getImageUrlForTask(long taskId) {
                return "http://lorempixel.com/600/400/cats/?fakeId=" + taskId;
            }
static class ViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    TextView titleView;
    ImageView imageView;
    public ViewHolder(CardView card) {
        super(card);
        cardView = card;
        titleView = (TextView)card.findViewById(R.id.text1);
        imageView = (ImageView)card.findViewById(R.id.image);
    }
}
}

TaskEditActivity activity to which I want to navigate from TaskListActivity:

package com.dummies.tasks.activity;

    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;



    public class TaskEditActivity extends AppCompatActivity {
        public static final String EXTRA_TASKID = "taskId";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_task_edit);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

    }

TaskListActivity or the main activity :

package com.dummies.tasks.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.dummies.tasks.interfaces.OnEditTask;

public class TaskListActivity extends AppCompatActivity implements OnEditTask {

    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);




    }
    @Override
    public void editTask(long id) {
// When we are asked to edit a reminder, start the
// TaskEditActivity with the id of the task to edit.
        startActivity(new Intent(this, TaskEditActivity.class)
                .putExtra(TaskEditActivity.EXTRA_TASKID, id));
    }
}

OnEditTask interface :

package com.dummies.tasks.interfaces;
public interface OnEditTask {
    /**
     * Called when the user asks to edit or insert a task.
     */
    public void editTask(long id);
}
like image 489
Shivam Panwar Avatar asked Aug 07 '16 12:08

Shivam Panwar


1 Answers

Your activities extend AppCompatActivity. Since Support library version 23.3.0 View.getContext() returns a TintContextWrapper object instead of an Activity. You can extract the Activity as described here: https://stackoverflow.com/a/32973351/6009935

like image 149
iridic Avatar answered Oct 29 '22 11:10

iridic