【问题标题】:No bugs in Java or XML code, but app crashes on launchJava 或 XML 代码中没有错误,但应用程序在启动时崩溃
【发布时间】:2021-01-21 11:05:01
【问题描述】:

我正在从课程构建项目应用程序。它在以前的版本中运行并成功启动,但是在我将教练的一些代码复制粘贴到地震列表项.xml 文件中后,该应用程序拒绝在模拟器和物理设备上启动。 Android Studio 没有向我显示任何可见的红线错误或错误。我检查了 logcat 并且有很多错误,我都无法理解。请帮忙。

这是地震列表项.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal"
    android:paddingEnd="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingStart="16dp">

    <TextView
        android:id="@+id/magnitude"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/magnitude_circle"
        android:fontFamily="sans-serif-medium"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        tools:text="8.9" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/location_offset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:fontFamily="sans-serif-medium"
            android:maxLines="1"
            android:textAllCaps="true"
            android:textColor="@color/textColorEarthquakeDetails"
            android:textSize="12sp"
            tools:text="30km S of" />

        <TextView
            android:id="@+id/primary_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="@color/textColorEarthquakeLocation"
            android:textSize="16sp"
            tools:text="Long placeholder location that should wrap to more than 2 lines of text" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textColor="@color/textColorEarthquakeDetails"
            android:textSize="12sp"
            tools:text="Mar 6, 2010" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textColor="@color/textColorEarthquakeDetails"
            android:textSize="12sp"
            tools:text="3:00 PM" />
    </LinearLayout>
</LinearLayout>

这些是 logcat 中的一些错误:

 java.lang.IllegalArgumentException: Context must contain application context.
        at com.miui.analytics.internal.policy.h.<init>(SourceFile:7)
        at com.miui.analytics.internal.policy.h.a(SourceFile:5)
        at com.miui.analytics.AnalyticsCore.getClientExtra(SourceFile:2)
        at com.miui.analytics.internal.util.g.b(SourceFile:2)
        at com.miui.analytics.internal.util.g.a(SourceFile:4)
        at com.miui.analytics.internal.LogEvent.a(SourceFile:40)
        at com.miui.analytics.internal.LogEvent.<init>(SourceFile:42)
        at com.miui.analytics.internal.collection.a.b(SourceFile:15)
        at com.miui.analytics.internal.ApkReceiver$1.run(SourceFile:6)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

编辑:我找到了问题和解决方案,这是由于我使用选择器而不是形状的 xml 文件。感谢所有试图提供帮助的人!

【问题讨论】:

  • No bugs in Java or XML code 运行时异常和编译时异常之间存在很大差异。仅仅因为您的应用程序可以编译,并不意味着它会按照您的预期工作。写作:thrown new RuntimeException() 也会编译,但是当你运行它时,那就是另一回事了 :)
  • 考虑添加活动类代码
  • 您的特定文件夹中是否有字体系列等所有值,并且您是否通过删除此 xml 来尝试您的代码。
  • 是的,我在它们各自的文件夹中有字体。添加此 xml 后,我使用的旧 xml 无法正常工作。

标签: java android xml android-studio android-layout


【解决方案1】:

您至少应该在主活动的 onCreate 中初始化上下文变量,在启动器活动的 onCreate 方法中尝试此操作context = activityName.this;

【讨论】:

  • 保持这样的上下文通常是一个糟糕的想法,而且是不必要的,无论您要传递该变量的任何地方,您都可以直接传入this
  • 对不起,我很长时间没有接触过 android,但我的回答的要点是告诉 OP,他可能忘记在他的活动中初始化上下文,因为我们看不到它。
  • since we can't see it. 看不到它,无法回答它:) 你(基本上)在这一点上猜测,所以在 OP 包含足够的信息来显示导致此问题的原因之前,我们可以't真的回答它
  • 是的,我猜,不过是有根据的猜测。
  • 我已经添加了活动代码,请大家帮忙。
猜你喜欢
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
相关资源
最近更新 更多