mac_05185 2022-06-23 17:33 采纳率: 67.1%
浏览 106
已结题

Android运行报错java.lang.ClassCastException?

img

编译运行时,报java.lang.ClassCastException,这个是什么原因导致的?下面是我的代码:

package com.example.car_custom_circleview;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    // 最多旋转一周,即360°
    private final float MAX_ROATE_DEGREE = 1.0f;

    private RoundProgressBar mRoundProgressBar1;
    private int progress = 0;
    // 当前浮点方向
    private float mDirection;
    // 目标浮点方向
    private float mTargetDirection;
    // 指南针view
    private CompassView mPointer;
    // 动画从开始到结束,变化率是一个加速的过程,就是一个动画速率
    private AccelerateInterpolator mInterpolator;
    private TextView TextViewSpd;

    protected final Handler mHandler = new Handler() {

        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case 1:
                    obtainMessage(1);
                    int arg1 = msg.arg1;
                    String spdShow = String.valueOf(arg1);
                    TextViewSpd.setText(spdShow);
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricle_progress);
        mRoundProgressBar1 = (RoundProgressBar) findViewById(R.id.roundProgressBar1);
        // 自定义的指南针view
        mPointer = (CompassView) findViewById(R.id.compass_pointer);
        // 实例化加速动画对象
        mInterpolator = new AccelerateInterpolator();
        // 初始化起始方向
        mDirection = 0.0f;
        // 初始化目标方向
        mTargetDirection = 0.0f;
        TextViewSpd = (TextView) findViewById(R.id.speed_show);
        ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new Thread(() -> {
                    while (true) {
                        progress += 2;
                        if (progress > 240) {
                            progress = 1;
                        }
                        mTargetDirection = progress * 360 / 240;
                        if (mDirection != mTargetDirection) {
                            // calculate the short routine
                            float to = mTargetDirection;
                            if (to - mDirection > 180) {
                                to -= 360;
                            } else if (to - mDirection < -180) {
                                to += 360;
                            }
                            // limit the max speed to MAX_ROTATE_DEGREE
                            float distance = to - mDirection;
                            if (Math.abs(distance) > MAX_ROATE_DEGREE) {
                                distance = distance > 0 ? MAX_ROATE_DEGREE
                                        : (-1.0f * MAX_ROATE_DEGREE);
                            }
                            // need to slow down if the distance is short
                            mDirection = normalizeDegree(mDirection
                                    + ((to - mDirection)));
                            mPointer.updateDirection(mDirection);// 更新指南针旋转
                        }
                        Message msg = new Message();
                        msg.what = 1;
                        msg.arg1 = progress;
                        mHandler.sendMessage(msg);
                        mRoundProgressBar1.setProgress(progress);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }).start();
            }
        });

    }

    /**
     * 调整方向传感器获取的值
     */
    private float normalizeDegree(float degree) {
        return (degree + 720) % 360;
    }
}

package com.example.car_custom_circleview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 自定义一个View继承ImageView,增加一个通用的旋转图片资源的方法
 */
@SuppressLint("AppCompatCustomView")
public class CompassView extends ImageView {
    private float mDirection;// 方向旋转浮点数
    private Drawable compass;// 图片资源

    //三个构造器
    public CompassView(Context context) {
        super(context);
        // 默认不旋转
        mDirection = 0.0f;
        compass = null;
    }

    public CompassView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDirection = 0.0f;
        compass = null;
    }

    public CompassView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mDirection = 0.0f;
        compass = null;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (compass == null) {
            compass = getDrawable();// 获取当前view的图片资源
            compass.setBounds(0, 0, getWidth(), getHeight());// 图片资源在view的位置,此处相当于充满view
        }

        canvas.save();
        canvas.rotate(mDirection, getWidth() / 2, getHeight() / 2);// 绕图片中心点旋转,
        compass.draw(canvas);// 把旋转后的图片画在view上,即保持旋转后的样子
        canvas.restore();// 保存一下
    }

    /**
     * 自定义更新方向的方法
     *
     * @param direction 传入的方向
     */
    public void updateDirection(float direction) {
        mDirection = direction;
        postInvalidate();// 重新刷新一下,更新方向
    }
}
  • 写回答

1条回答 默认 最新

  • dakeSeven 2022-06-23 18:44
    关注

    这个报错的意思是类型不能强转,楼主可以看下view的id定义是不是有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月27日
  • 已采纳回答 6月27日
  • 创建了问题 6月23日

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)