Logcat 错误:应用程序崩溃且无法运行.根据 Logcat 在 setContentView 处出错 [英] Logcat Error: App Crashes and does not run. Error at setContentView as per Logcat

查看:46
本文介绍了Logcat 错误:应用程序崩溃且无法运行.根据 Logcat 在 setContentView 处出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a basic equation balancing app and am new to android dev. The app does not run on the phone at all. I had a splash screen as launcher previously and that used to pop and the app would shut off after that. I removed the Splash Screen and now the app won't open. I'm guessing there is an error in MainActivity (the splash screen used to intent to it and now it is the launcher activity). But on running the logcat alongside ADB emulation, it shows error in onCreate method of MainActivity. There are some more errors in logcat but I don't know what they mean(I guess the only error that matters is the onCreate one with a blue underline). Please Please do Help.

MainActivity.java

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

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

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(app):

build.gradle(app):

<?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)

Logcat Here's the underlined line in 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

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

If any other files are needed, I am ready to provide. I just want to know what is happening and sort it out. Thanks!

PS:其中哪个是实际转换为 apk 的文件夹?是主文件夹,化学方程式平衡器吗?

主题.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,这意味着它继承了默认操作栏.

Your MainActivity is extending AppCompatActivity which means it inherits the default action bar.

ThemeOverlay.AppCompat(parent forTheme.ChemicalEquationBalancer.ActionBar) 用于覆盖(或覆盖")特定视图的主题,尤其是工具栏.

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 布局提供一个工具栏.

If you want to customize this ActionBar (you are probably doing that), you add one of the NoActionBar themes in your activity's manifest and then supply a toolbar to your activity's layout.

    <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中,你可以添加任何你想要的主题

In your activity_main.xml, you can add whatever theme you want

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

并且不在清单文件中

这篇关于Logcat 错误:应用程序崩溃且无法运行.根据 Logcat 在 setContentView 处出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆