mac_05185
2022-06-24 09:27
采纳率: 75.6%
浏览 23

Android报Binary XML file line #8: Error

img


这大概是什么原因导致的?错误如下:


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.easyboardview/com.example.easyboardview.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class android.widget.ImageView

这部分是我的代码:

package com.example.easyboardview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {
    //指针图片
    private ImageView needleView;
    //时间
    private Timer timer;

    //记录指针旋转
    private float degree = 0.0f;

    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //设置仪表盘指针转动动画
            //仪表盘最大是172度,这个是自己测出来的
            if (degree >= 172.0f) {
                timer.cancel();
            }
            degree += 2.0f;
            RotateAnimation animation = new RotateAnimation(degree, degree, Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(1000);
            animation.setFillAfter(true);
            needleView.startAnimation(animation);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        needleView = findViewById(R.id.needle);
        // 开始转动
        timer = new Timer();
        // 设置每一秒转动一下
        timer.schedule(new NeedleTask(), 0, 1000);
    }

    private class NeedleTask extends TimerTask {
        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    }
}

这部分是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="270dp"
        android:layout_height="270dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:src="@drawable/panel" />

    <ImageView
        android:id="@+id/needle"
        android:layout_width="270dp"
        android:layout_height="270dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:src="@drawable/needle" />

</RelativeLayout>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题