运行安卓程序报错android.view.InflateException: Binary XML file line #11: Error inflating class ImageView
先上代码
布局代码<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="6"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageQQ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/qqimage"
android:layout_rowSpan="6">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="85dp"
android:id="@+id/username">
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="85dp"
android:id="@+id/password"
android:inputType="textPassword">
</EditText>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_columnSpan="2"
android:id="@+id/rg">
<RadioButton
android:text="男"
android:checked="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RadioButton>
<RadioButton android:text="女"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RadioButton>
</RadioGroup>
<CheckBox
android:id="@+id/savepassword"
android:text="记住密码"></CheckBox>
<CheckBox
android:id="@+id/autologin"
android:text="自动登录"></CheckBox>
<Button
android:text="注册"
android:id="@+id/register">
</Button>
<Button
android:text="登录"
android:id="@+id/login">
</Button>
<TextView android:id="@+id/show"
android:text=""
android:layout_columnSpan="2"></TextView>
</GridLayout>
java代码
public class MainActivity extends AppCompatActivity {
EditText userName;
EditText passWord;
RadioGroup rg;
CheckBox savePassword;
CheckBox autoLogin;
TextView show;
Button loginButton;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.username);
passWord = (EditText) findViewById(R.id.password);
rg = (RadioGroup) findViewById(R.id.rg);
savePassword = (CheckBox) findViewById(R.id.savepassword);
autoLogin = (CheckBox) findViewById(R.id.autologin);
show = (TextView) findViewById(R.id.show);
loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = userName.getText().toString();
String password = passWord.getText().toString();
String sex = null;
boolean savepassword = savePassword.isChecked();
boolean autologin = autoLogin.isChecked();
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton rb = (RadioButton) rg.getChildAt(i);
if (rb.isChecked()) {
sex = (String) rb.getText();
break;
}
}
show.setText("用户名:" + username + "\n" +
"密码:" + password + "\n" +
"性别" + sex + "\n" +
"记住密码" + savepassword + "\n" +
"自动登录" + autologin);
}
});
}
}
问题
上午经历过软测比赛的挫败,感觉只能无奈写写安卓实验。边看B站教程边看书边敲代码,总算布局搞定了,组件搞定了,监听器内容也搞定了。 在满心期待的心情下点下了run去运行虚拟机,结果app打开后立马弹出一个框。
我:wtf这什么情况???
好吧,作为一个成熟的准程序员,是不可能被bug给打败的,程序员的工作不就是改 (写) bug吗,我怎么可能就此止步,于是开始了我剩余三个小时的debug之旅。网上各种查技术博客与论坛,有人说是代码写错了,而且还是不会报错的那种。
好吧,成熟的准程序员不可能被打败。又花了一个小时看代码,没错啊。
我:玩我???
又翻了一会博客,在我即将放弃做一名成熟的准程序员时,我发现了营救我职业的曙光。
问题及解决方法
在logcat我发现在这个报错:android.view.InflateException: Binary XML file line #11: Error inflating class ImageView,说明ImageView出错了,这部分代码不多,于是上网对这上面搜了下。
自己在开发过程中一般习惯都在Android模式下,res目录下文件夹只有几个。切换到在Project模式下,res目录下除了drawable文件夹、还有drawable-v24文件夹,发现自己的图片竟然是放在drawable-v24下,导致在低分辨率手机上无法找到图片而报错,可能问题就出在这里。
再加上我的SDK虽然是Android 29(安卓9),但是我的运行环境是安卓5.0,默认去寻找drawable文件夹下的资源,这样就造成了在运行时找不到这个资源。
从Android API 24(安卓7.0)开始,自定义Drawables类可以最终在XML中使用,不同的drawable文件夹用于为设备兼容性和不同的Android版本提供不同的屏幕密度。
解决方法很简单,把你的资源放进drawable文件夹,不要放在drawable-v24文件夹下,否则安卓找不到资源,就会认为你写错了。