ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams里面的活动

时间:2017-02-08 18:14:27

标签: android android-activity android-recyclerview classcastexception

我试图点击在adapterview中声明的按钮 我正在内部活动中加载recyclerview 当我执行click并尝试从mRecyclerView.getChildLayoutPosition方法获取值时,它给我classcastexception

这是我的活动代码

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_reminder_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        dbAdapter=new LocationReminderDBAdapter(getApplicationContext());
        dbAdapter.open();
        /////////////////////////////////////////////////////////////////////////////////////
        mRecyclerView = (RecyclerView)findViewById(R.id.reminders_recycler);

        int[] locationImage = new int[5];
        for (int i = 0; i < locationImage.length; i++) {
            locationImage[i] = i;
        }
        reminderNames = new ArrayList<String>();
        reminderLocations = new ArrayList<String>();
        imagePaths=new ArrayList<String>();
        mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        adapter = new SavedReminderAdapter(reminderNames, reminderLocations, imagePaths);
        mRecyclerView.setAdapter(adapter);
        populateReminderList();

        adapter.setListener(new SavedReminderAdapter.Listener() {
            @Override
            public void onClick(int position) {
                Intent intent=new Intent(getApplicationContext(),ViewAddedReminder.class);
                startActivity(intent);
            }


        });
        //////////////////////////////////////////////////////////////////////////////////////
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LocationReminderList.this,AddLocationReminder.class);
                startActivity(intent);
               /* Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();*/
            }
        });


    }

这里给出了classcastexception错误

public void editReminder(View view){
        int itemPosition = mRecyclerView.getChildLayoutPosition(view);

    }

这是我的活动布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_location_reminder_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="2dp"
    android:paddingLeft="0.5dp"
    android:paddingRight="0.5dp"
    android:paddingTop="2dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="reminder.locrem.com.locationreminder.LocationReminderList"
    tools:showIn="@layout/app_bar_location_reminder_list">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="118dp"
        android:layout_marginTop="87dp"
        android:id="@+id/tempshow" />

    <include
        layout="@layout/recycler_view_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</RelativeLayout>

这是我的回收站视图布局代码(recycler_view_list)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/reminders_recycler"
    android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>

最后这是我的适配器视图的布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/reminder_card_view"
    android:layout_margin="5dp"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textSize="18sp"
            android:textAlignment="viewStart"
            android:id="@+id/list_of_items_label"
            android:textStyle="normal|bold"
            android:text="Items List" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textSize="14sp"
            android:textAlignment="viewStart"
            android:id="@+id/list_of_items"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textSize="18sp"
            android:textAlignment="viewStart"
            android:id="@+id/location_label"
            android:textStyle="normal|bold"
            android:text="Location" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textSize="14sp"
            android:maxLines="1"
            android:ellipsize="end"
            android:textAlignment="viewStart"
            android:id="@+id/location_value"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/info_image"
            android:layout_weight="0.47"
            android:scaleType="centerCrop"
            />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="32dp">
        <ImageButton
            android:id="@+id/reminder_alarm"
            android:background="@android:drawable/ic_lock_idle_alarm"
            android:layout_width="31dp"
            android:layout_height="31dp"
            android:onClick="setAlarm"/>
        <ImageButton
            android:layout_width="31dp"
            android:layout_height="31dp"
            android:id="@+id/markCompleted"
            android:layout_centerHorizontal="true"
            android:background="@drawable/done"
            android:backgroundTint="@color/common_google_signin_btn_text_light_default"
            android:onClick="settaskCompleted"/>
        <ImageButton
            android:id="@+id/edit_reminder"
            android:layout_alignParentRight="true"
            android:background="@android:drawable/ic_menu_edit"
            android:layout_width="31dp"
            android:layout_height="31dp"
            android:onClick="editReminder"/>
        </RelativeLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

build.gradle文件

compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services-maps:9.2.0'
    compile 'com.google.android.gms:play-services:9.2.0'
    compile 'com.google.android.gms:play-services-ads:9.2.0'
    compile 'com.google.android.gms:play-services-auth:9.2.0'
    compile 'com.google.android.gms:play-services-gcm:9.2.0'
    compile 'com.android.support:cardview-v7:24.2.1'

请帮我解决这个问题 提前谢谢

2 个答案:

答案 0 :(得分:0)

问题实际上与您指向的方法相同

public void editReminder(View view){
        int itemPosition = mRecyclerView.getChildLayoutPosition(view);
}

getChildLayoutPosition内部调用以下方法

static ViewHolder getChildViewHolderInt(View child) {
        if (child == null) {
            return null;
        }
        return ((LayoutParams) child.getLayoutParams()).mViewHolder;
}

正如您在上面所看到的,它正在尝试将LayoutParams转换为RecyclerView.LayoutParams,并且您的视图类为RelativeLayout,从而在此处抛出类强制转换异常。

现在让我们谈谈解决方案

我宁愿建议您通过在标记中设置来获取视图ID的简单方法。

onBindViewHolder设置ID

@Override
public void onBindViewHolder(ViewHolder customViewHolder, int i) {
   // do your stuff
   viewHolder.itemView.setTag(<SET_YOUR_ID>);
}

并将其取回

public void editReminder(View view){
    int itemPosition = (int)view.getTag();
}

答案 1 :(得分:-1)

您使用的是哪个版本的支持库?支持库v24.2.0或更早版本中存在已知错误导致此问题。尝试更新到更新的版本,看看是否有帮助。版本24.2.1为我工作。