運行安卓程序報錯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文件夾下,否則安卓找不到資源,就會認爲你寫錯了。