gpt4 book ai didi

Android.support.v7.widget.TintContextWrapper 无法转换

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:35 25 4
gpt4 key购买 nike

我正在按照一些教程制作任务提醒应用程序。它有两个 Activity ,即 TaskListActivity 和 TaskEditActivity。 TaskList Activity 运行良好,但当我尝试使用 intent 导航到 TaskEditActivity 时,它崩溃了。下面是我的 list 文件:

<?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>

下面是我的 build.gradle 文件:

 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'

}

它的Logcat如下:

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

适配器类:

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

我想从 TaskListActivity 导航到的 TaskEditActivity Activity :

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 或主要 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);
}

最佳答案

您的 Activity 扩展 AppCompatActivity。由于支持库版本 23.3.0 View.getContext() 返回一个 TintContextWrapper 对象而不是 Activity。您可以按照此处所述提取 Activity:https://stackoverflow.com/a/32973351/6009935

关于Android.support.v7.widget.TintContextWrapper 无法转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com