我正在Android Studio中制作一个应用程序,在向MainActivity添加按钮以移动到其他活动后,尽管事实上布局在编辑器中显示正确,但我得到了错误,将在最后发布。在我的项目中还有其他的活动,我没有包括在内,因为错误指的是activity_main.xml,但如果它们是解决这个问题所必需的,我将包括它们。
这里是activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/view_img_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/grocery_img_1" />
<Button
android:id="@+id/btn_inventory"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="@id/view_img_main_menu"
android:text="@string/btn_my_groceries_text"
android:theme="@style/BtnTheme" />
<Button
android:id="@+id/btn_shopping_list"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="@id/btn_inventory"
android:text="@string/btn_shopping_list_text"
android:theme="@style/BtnTheme" />
</RelativeLayout>
这里是MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btnInventory;
private Button btnShopList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInventory = findViewById(R.id.btn_inventory);
btnShopList = findViewById(R.id.btn_shopping_list);
btnInventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent inventoryIntent = new Intent(MainActivity.this, Inventory.class);
startActivity(inventoryIntent);
}
});
btnShopList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shopListIntent = new Intent(MainActivity.this, ShoppingList.class);
startActivity(shopListIntent);
}
});
}
这是我在运行代码时得到的错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grocerystatusapplication/com.example.grocerystatusapplication.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Han
E/AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956)
这里是 styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/colorLayoutBackground</item>
</style>
<style name="BtnTheme" parent="TextAppearance.AppCompat.Widget.Button.Colored">
<item name="colorButtonNormal">@color/colorViewBackground</item>
</style>
<style name="TextViewTheme" parent="">
<item name="android:background">@drawable/rounded_corners</item>
<item name="android:textColor">@android:color/white</item>
</style>
我试着改变错误所指的Button的属性(第15行,btn_inventory),但没有任何进展。以前没有遇到过这个错误,也不太明白问题出在哪里。我该怎么改才能让它正常工作?看了一下关于同样错误的帖子,我的问题没有得到答案,这个错误出现的原因似乎有不少。
在两个按钮标签改变
android:theme="@style/BtnTheme"
到
style="@style/BtnTheme"