Android Studio Android应用程序崩溃,出现“Error inflating class Button”

jqjz2hbq  于 2023-06-06  发布在  Android
关注(0)|答案(1)|浏览(157)

我只是有一个正常的按钮视图在我的日程活动从那里我去另一个活动和更新字段,并使API调用,但去那里onClick我得到无法解释的错误。如何理解代码和错误?

UpdateSchedule.class

public class UpdateSchedule extends AppCompatActivity {

    Button btnAddRoute, btnAddSource, btnRouteInfo, btnScheduleInfo;

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

        initViews();
    }

activity_update_schedule.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:context=".activity.UpdateSchedule">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@android:color/holo_orange_light"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/wave__1_"/>

    <Button
        android:id="@+id/add_route"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="80dp"
        android:text="Add Route" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Add Halt"
        android:id="@+id/add_source"/>

    <Button
        android:layout_marginTop="20dp"
        android:id="@+id/btn_route_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="add route info" />

    <Button
        android:id="@+id/btn_schedule_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="add schedule info" />

    <!--    <FrameLayout-->
<!--        android:id="@+id/frameLayout_schedule_container"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="match_parent" />-->

</LinearLayout>

这是我的日志错误

FATAL EXCEPTION: main
Process: com.example.sts_admin, PID: 30636
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sts_admin/com.example.sts_admin.activity.UpdateSchedule}: android.view.InflateException: Binary XML file line #25 in com.example.sts_admin:layout/activity_update_schedule: Binary XML file line #25 in com.example.sts_admin:layout/activity_update_schedule: Error inflating class Button
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3846)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    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:2336)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.view.InflateException: Binary XML file line #25 in com.example.sts_admin:layout/activity_update_schedule: Binary XML file line #25 in com.example.sts_admin:layout/activity_update_schedule: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #25 in com.example.sts_admin:layout/activity_update_schedule: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException: File res/color/mtrl_btn_text_color_selector.xml from ComplexColor resource ID #0x7f0602a7
    at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1421)
    at android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:1247)
    at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1326)
    at android.content.res.Resources.loadColorStateList(Resources.java:1249)
    at android.content.res.TypedArray.getColorStateList(TypedArray.java:600)
    at android.widget.TextView.readTextAppearance(TextView.java:4326)
    at android.widget.TextView.<init>(TextView.java:1360)
    at android.widget.Button.<init>(Button.java:174)
    at android.widget.Button.<init>(Button.java:149)
    at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:81)
    at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:228)
    at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:224)
    at com.google.android.material.theme.MaterialComponentsViewInflater.createButton(MaterialComponentsViewInflater.java:43)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:156)
    at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1678)
    at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1729)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:775)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197)
    at com.example.sts_admin.activity.UpdateSchedule.onCreate(UpdateSchedule.java:25)
    at android.app.Activity.performCreate(Activity.java:8207) 2023-06-05 14:27:22.679 30636-30636 AndroidRuntime          com.example.sts_admin E   at android.app.Activity.performCreate(Activity.java:8191)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3819)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    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:2336)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 0 to color: type=0x1
    at android.content.res.TypedArray.getColor(TypedArray.java:533)
    at android.content.res.ColorStateList.inflate(ColorStateList.java:332)
    at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:280)
    at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1414)
    ... 41 more
2uluyalo

2uluyalo1#

我想这是你的问题:(ofc可以帮助我们了解initViews()的作用)

public class UpdateSchedule extends AppCompatActivity {

    Button btnAddRoute, btnAddSource, btnRouteInfo, btnScheduleInfo;

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

        btnAddRoute = findViewById(R.id.add_route);
        // do it for all buttons

        initViews();
    }

相关问题