我正在制作一个任务提醒应用程序,遵循一些教程。它有两个活动,即TaskListActivity和TaskEditActivity。 TaskList活动运行正常,但是当我尝试使用intent导航到TaskEditActivity时,它崩溃了。 以下是我的清单文件:
<?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活动:
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或主要活动:
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接口:
package com.dummies.tasks.interfaces;
public interface OnEditTask {
/**
* Called when the user asks to edit or insert a task.
*/
public void editTask(long id);
}
答案 0 :(得分:24)
您的活动延长AppCompatActivity
。由于支持库版本23.3.0 View.getContext()
会返回TintContextWrapper
个对象而不是Activity
。您可以按照此处所述提取Activity
:https://stackoverflow.com/a/32973351/6009935