FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #13: Error inflating class progressBar
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.progressBar"
XML 布局中標籤名區分大小寫,必須與類名完全一致。
正確:<ProgressBar>(P、B 大寫)
錯誤:<progressBar>(全小寫)
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進度條" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/progressBarHorizontal"
android:max="100" />
<Button android:id="@+id/btnup" ... />
<Button android:id="@+id/btndown" ... />
</LinearLayout>
// 成員變量引用同一個 ProgressBar
private ProgressBar progressBarHorizontal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBarHorizontal = findViewById(R.id.progressBarHorizontal);
// 移除錯誤的 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// 移除錯誤的 setProgressBarIndeterminateVisibility(true);
Button btnup = findViewById(R.id.btnup);
Button btndown = findViewById(R.id.btndown);
btnup.setOnClickListener(this);
btndown.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int step = 5;
if (view.getId() == R.id.btnup) {
progressBarHorizontal.incrementProgressBy(step);
} else if (view.getId() == R.id.btndown) {
progressBarHorizontal.incrementProgressBy(-step);
}
int current = progressBarHorizontal.getProgress();
Toast.makeText(this, "當前進度:" + current, Toast.LENGTH_SHORT).show();
}
ClassNotFoundException。incrementProgressBy 封裝了邊界檢查,比手動 setProgress 更簡潔。