Logcat Error: App Crashes and does not run. Error at setContentView as per Logcat

Logcat Error: App Crashes and does not run. Error at setContentView as per Logcat

我正在开发一个基本的方程式平衡应用程序,并且是 android 开发人员的新手。该应用根本不 运行 phone。我之前有一个启动画面作为启动器,它曾经弹出,之后应用程序将关闭。我删除了启动画面,现在应用程序无法打开。我猜 MainActivity 中有一个错误(启动画面曾经是它的意图,现在它是启动器 activity)。但是在 运行 与 ADB 仿真一起使用 logcat 时,它在 MainActivity 的 onCreate 方法中显示错误。 logcat 中还有一些错误,但我不知道它们是什么意思(我想唯一重要的错误是带有蓝色下划线的 onCreate 错误)。请帮忙。

MainActivity.java

public class MainActivity extends AppCompatActivity implements DialogCloseListener {

    private RecyclerView taskRecyclerView;
    private EqBalAdapter tasksAdapter;
    private List<EqBalModel> taskList;
    private DatabaseHandler db;
    private FloatingActionButton fab;
    private FloatingActionButton fab2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar bar = getActionBar();

        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00d024")));

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.nav_bottom);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_home:
                        break;
                    case R.id.nav_aboutus:
                        Intent a = new Intent(MainActivity.this, AboutUs.class);
                        startActivity(a);
                        break;
                    case R.id.nav_help:
                        Intent b = new Intent(MainActivity.this,HelpGuide.class);
                        startActivity(b);
                        break;

                }
                return false;
            }
        });


        db = new DatabaseHandler(this);
        db.openDatabase();

        taskList = new ArrayList<>();

        taskRecyclerView = findViewById(R.id.reacRecyclerView);
        taskRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        tasksAdapter = new EqBalAdapter(db, this);
        taskRecyclerView.setAdapter(tasksAdapter);

        fab = findViewById(R.id.fab);

        ItemTouchHelper itemTouchHelper = new
                ItemTouchHelper(new RecyclerItemTouchHelper(tasksAdapter));
        itemTouchHelper.attachToRecyclerView(taskRecyclerView);

        taskList = db.getAllTasks();
        Collections.reverse(taskList);
        tasksAdapter.setTasks(taskList);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddNewTask.newInstance().show(getSupportFragmentManager(), AddNewTask.TAG);
            }
        });
        fab2=findViewById(R.id.newactivity);

        ItemTouchHelper itemTouchHelper2 = new
                ItemTouchHelper(new RecyclerItemTouchHelper(tasksAdapter));
        itemTouchHelper2.attachToRecyclerView(taskRecyclerView);


        fab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this, eleView.class);
                startActivity(intent2);
            }
        });
    }

    @Override
    public void handleDialogClose(DialogInterface dialog) {
        taskList = db.getAllTasks();
        Collections.reverse(taskList);
        tasksAdapter.setTasks(taskList);
        tasksAdapter.notifyDataSetChanged();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/ReactantText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="Equation Balancer"
        android:textColor="@android:color/black"
        android:textSize="32dp"
        android:textStyle="bold" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/reacRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ReactantText"
        android:nestedScrollingEnabled="true"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="183dp"
        android:backgroundTint="@android:color/holo_green_dark"
        android:src="@drawable/ic_baseline_add" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/newactivity"
        android:layout_width="86dp"
        android:layout_height="82dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="56dp"
        android:layout_marginTop="56dp"
        android:layout_marginEnd="34dp"
        android:layout_marginBottom="97dp"
        android:backgroundTint="@android:color/holo_green_dark"
        android:src="@drawable/ic_baseline_elements" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nav_bottom"
        android:layout_alignParentBottom="true"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/drawer_view" />
</RelativeLayout>

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chemicalequationbalancer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ChemicalEquationBalancer">


        <activity android:name="com.example.chemicalequationbalancer.AboutUs" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
        <activity android:name="com.example.chemicalequationbalancer.HelpGuide" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
        <activity android:name="com.example.chemicalequationbalancer.eleView" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>


        <activity
            android:name="com.example.chemicalequationbalancer.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.ChemicalEquationBalancer.ActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>


</manifest>

build.gradle(应用程序):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chemicalequationbalancer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ChemicalEquationBalancer">


        <activity android:name="com.example.chemicalequationbalancer.AboutUs" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
        <activity android:name="com.example.chemicalequationbalancer.HelpGuide" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
        <activity android:name="com.example.chemicalequationbalancer.eleView" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>


        <activity
            android:name="com.example.chemicalequationbalancer.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.ChemicalEquationBalancer.ActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>


</manifest>

Logcat 这是 logcat 中带下划线的行:com.example.chemicalequationbalancer.MainActivity.onCreate(MainActivity.java:51)

2021-02-16 02:07:50.712 13502-13502/com.example.chemicalequationbalancer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chemicalequationbalancer, PID: 13502
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chemicalequationbalancer/com.example.chemicalequationbalancer.MainActivity}: android.view.InflateException: Binary XML file line #31: Binary XML file line #31: Error inflating class com.android.internal.widget.ActionBarContainer
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3197)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3334)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:226)
        at android.app.ActivityThread.main(ActivityThread.java:7191)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
     Caused by: android.view.InflateException: Binary XML file line #31: Binary XML file line #31: Error inflating class com.android.internal.widget.ActionBarContainer
     Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class com.android.internal.widget.ActionBarContainer
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:652)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:817)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:890)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:851)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at com.android.internal.policy.DecorView.onResourcesLoaded(DecorView.java:2239)
        at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2761)
        at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2868)
        at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2133)
        at androidx.appcompat.app.AppCompatActivity.initViewTreeOwners(AppCompatActivity.java:198)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:173)
        at **com.example.chemicalequationbalancer.MainActivity.onCreate(MainActivity.java:51)**
        at android.app.Activity.performCreate(Activity.java:7376)
        at android.app.Activity.performCreate(Activity.java:7367)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3177)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3334)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:226)
        at android.app.ActivityThread.main(ActivityThread.java:7191)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
2021-02-16 02:07:50.714 13502-13502/com.example.chemicalequationbalancer E/AndroidRuntime: Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x1010433 a=1}
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
        at android.view.View.<init>(View.java:5108)
        at android.view.ViewGroup.<init>(ViewGroup.java:665)
        at android.widget.FrameLayout.<init>(FrameLayout.java:92)
        at android.widget.FrameLayout.<init>(FrameLayout.java:87)
        at android.widget.FrameLayout.<init>(FrameLayout.java:82)
        at com.android.internal.widget.ActionBarContainer.<init>(ActionBarContainer.java:61)
            ... 32 more

如果需要任何其他文件,我准备提供。我只想知道发生了什么并解决它。谢谢!

PS: Which of these is the folder which actually gets converted to apk? Is it main folder, the chemicalequationbalancer?

编辑: themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.ChemicalEquationBalancer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@android:color/transparent</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.ChemicalEquationBalancer.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="Theme.ChemicalEquationBalancer.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="Theme.ChemicalEquationBalancer.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
    </style>

    <style name="Theme.ChemicalEquationBalancer.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    <style name="DialogStyle" parent="Theme.Design.BottomSheetDialog">
        <item name="android:windowIsFloating">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
    </style>

</resources>

您的 MainActivity 正在扩展 AppCompatActivity,这意味着它继承了默认操作栏。

ThemeOverlay.AppCompat(parent for Theme.ChemicalEquationBalancer.ActionBar) is used to override (or “overlay”) that theme for specific views, especially the Toolbar.

如果您想自定义此 ActionBar(您可能正在这样做),请在 activity 的清单中添加一个 NoActionBar 主题,然后为您的 activity 提供一个工具栏布局。

    <activity
        android:name="com.example.chemicalequationbalancer.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Light.NoActionBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

在您的 activity_main.xml 中,您可以添加任何您想要的主题

<androidx.appcompat.widget.Toolbar
    ...
    android:background="?attr/colorPrimary"
    android:theme="@style/Theme.ChemicalEquationBalancer.ActionBar />

并且不在清单文件中