Java或XML代码中没有bug,但应用程序在启动时崩溃
我正在根据课程构建一个项目应用程序。它在上一版本中成功运行并启动,但在我复制并粘贴了讲师在地震列表项目.xml文件中的一些代码后,该应用拒绝在模拟器和物理设备上启动。Android Studio没有向我显示任何可见的红线错误或bug。我已经检查了logcat,有很多错误,我都不理解。请帮忙 这是地震列表项目.xml文件:Java或XML代码中没有bug,但应用程序在启动时崩溃,java,android,xml,android-studio,android-layout,Java,Android,Xml,Android Studio,Android Layout,我正在根据课程构建一个项目应用程序。它在上一版本中成功运行并启动,但在我复制并粘贴了讲师在地震列表项目.xml文件中的一些代码后,该应用拒绝在模拟器和物理设备上启动。Android Studio没有向我显示任何可见的红线错误或bug。我已经检查了logcat,有很多错误,我都不理解。请帮忙 这是地震列表项目.xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:
<?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)
java.lang.IllegalArgumentException:上下文必须包含应用程序上下文。
在com.miui.analytics.internal.policy.h.(源文件:7)
位于com.miui.analytics.internal.policy.h.a(源文件:5)
在com.miui.analytics.AnalyticsCore.getClientExtra(源文件:2)
位于com.miui.analytics.internal.util.g.b(源文件:2)
位于com.miui.analytics.internal.util.g.a(源文件:4)
位于com.miui.analytics.internal.LogEvent.a(源文件:40)
位于com.miui.analytics.internal.LogEvent。(源文件:42)
位于com.miui.analytics.internal.collection.a.b(源文件:15)
位于com.miui.analytics.internal.ApkReceiver$1.run(源文件:6)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
运行(Thread.java:919)
编辑:我已经找到了问题和解决方案,这是由于一个xml文件,我使用选择器而不是形状。感谢所有试图帮忙的人 至少应该在onCreate中为主活动初始化上下文变量,在启动器活动的onCreate方法中尝试此操作
context=activityName.this代码>Java或XML代码中没有bug
运行时异常和编译时异常之间有很大区别。仅仅因为你的应用程序可以编译,并不意味着它会像你期望的那样工作。编写:抛出新的RuntimeException()
也会编译,但当您运行它时,情况就不同了:)考虑添加活动类代码您是否在特定文件夹中有字体系列之类的所有值,并且您是否通过删除此xml尝试过代码。是的,我在各自的文件夹中有字体。添加此xml后,我使用的旧xml不起作用。像这样保留上下文通常是一个糟糕的想法,而且没有必要,无论您在哪里传递该变量,您都可以直接传入此
我很久没有接触android了抱歉,但我回答的要点是告诉OP,他可能忘了在他的活动中初始化上下文,因为我们看不见它。因为我们看不见它。
看不见,不能回答:)你(基本上)在这一点上是在猜测,所以直到OP包含足够的信息来显示是什么导致了这个问题,我们才真正回答它是的,我在猜测,不过这是一个有根据的猜测。我已经添加了活动代码,请帮助大家。