java.lang.ClassCastException: com.example.kada.view.pzv.PullToZoomScrollViewEx 无法转换为 com.example.kada.view.pzv.PullToZoomListViewEx

java.lang.ClassCastException: com.example.kada.view.pzv.PullToZoomScrollViewEx cannot be cast to com.example.kada.view.pzv.PullToZoomListViewEx

启动这个 activity 时我收到这个尴尬的错误

java.lang.ClassCastException: com.example.com.kada.view.pzv.PullToZoomScrollViewEx cannot be cast to com.example.com.kada.view.pzv.PullToZoomListViewEx

我的Activity(缩放Activity)

package com.example.com.kada;

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.widget.ListView;

import com.example.com.kada.adhss.PullToZoomScrollViewEx;
import com.example.com.kada.view.pzv.PullToZoomListViewEx;


public class ZoomActivity extends AppCompatActivity {

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


        PullToZoomListViewEx listView = (PullToZoomListViewEx) findViewById(R.id.paralax_social_list_view);
       listView.setShowDividers(0);

       // PullToZoomListViewEx listView = (PullToZoomListViewEx) findViewById(R.id.paralax_social_list_view);
       // listView.setShowDividers(0);

    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_blog_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/hss_background"
    tools:context="com.example.kada.blogListActivity">


    <com.example.kada.view.pzv.PullToZoomScrollViewEx
        android:id="@+id/paralax_social_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        app:headerView="@layout/header_parallax_social" />




</RelativeLayout>

如何解决这个问题,尽管我将相同的代码放在服务依赖项的其他项目中 com.android.support:support-v4:21.0.3 并且看起来不错

现在我正在使用 com.android.support:appcompat-v7:25.3.1

com.example.com.kada.view.pzv.PullToZoomScrollViewEx 不是 com.example.com.kada.view.pzv.PullToZoomListViewEx 的子 class,因此无法转换。这就是异常消息明确试图告诉您的内容。

您的布局使用 PullToZoomScrollViewEx,但您将 findViewById() returns 转换为 PullToZoomListViewEx。修复转换以使用正确的 class 或确保 class 具有相同的祖先。

此外,如果您切换到使用 SDK 26 进行编译,请参阅 ,但是如果您使用错误 classes.

,这仍然会抛出异常