【问题标题】:How to debug a NullPointerException in an Android app?如何在 Android 应用程序中调试 NullPointerException?
【发布时间】:2012-02-08 14:10:37
【问题描述】:

我有一个非常简单的应用程序无法正常启动。该应用程序应加载启动画面,然后是主应用程序。由于某种原因,它现在无法正常工作 -

Logcat:

    02-08 13:30:41.846: E/AndroidRuntime(275): Uncaught handler: thread main exiting due to uncaught exception
02-08 13:30:41.865: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KES.GApps/com.KES.GApps.Splashscreen}: java.lang.NullPointerException
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.os.Looper.loop(Looper.java:123)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.ActivityThread.main(ActivityThread.java:4363)
02-08 13:30:41.865: E/AndroidRuntime(275):  at java.lang.reflect.Method.invokeNative(Native Method)
02-08 13:30:41.865: E/AndroidRuntime(275):  at java.lang.reflect.Method.invoke(Method.java:521)
02-08 13:30:41.865: E/AndroidRuntime(275):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-08 13:30:41.865: E/AndroidRuntime(275):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-08 13:30:41.865: E/AndroidRuntime(275):  at dalvik.system.NativeStart.main(Native Method)
02-08 13:30:41.865: E/AndroidRuntime(275): Caused by: java.lang.NullPointerException
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1860)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.view.ViewGroup.addView(ViewGroup.java:1756)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.view.ViewGroup.addView(ViewGroup.java:1736)
02-08 13:30:41.865: E/AndroidRuntime(275):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:217)
02-08 13:30:41.865: E/AndroidRuntime(275):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.Activity.setContentView(Activity.java:1633)
02-08 13:30:41.865: E/AndroidRuntime(275):  at com.KES.GApps.Splashscreen.onCreate(Splashscreen.java:13)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 13:30:41.865: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-08 13:30:41.865: E/AndroidRuntime(275):  ... 11 more
02-08 13:30:41.884: I/dalvikvm(275): threadid=7: reacting to signal 3
02-08 13:30:41.884: E/dalvikvm(275): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

初始屏幕的 xml (welcome.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/img1"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_gravity="fill_horizontal"
        android:contentDescription="@string/crestinfo"
        android:src="@drawable/logov2" />

    <TextView
        android:id="@+id/welcomescreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="@string/welcomescreen"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:textStyle="bold" android:padding="5dp" android:textSize="15pt" />

</LinearLayout>

java:

package com.KES.GApps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


public class Splashscreen extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        Thread splashThread = new Thread() {
            @Override
            public void run() {
               try {
                  int waited = 0;
                  while (waited < 2000) {
                     sleep(100);
                     waited += 100;
                  }
               } catch (InterruptedException e) {
                  // do nothing
               } finally {
                  finish();
                  Intent i = new Intent();
                  i.setClassName("com.KES.GApps",
                                 "com.KES.GApps.KingEdwardVIISchoolActivity");
                  startActivity(i);
               }
            }
         };
         splashThread.start();
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.KES.GApps"
    android:versionCode="6"
    android:versionName="1.5" >

    <uses-sdk android:minSdkVersion="7" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name="KingEdwardVIISchool"
        android:debuggable="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:launchMode="standard" >
        <activity
            android:name="KingEdwardVIISchoolActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity
            android:name="Splashscreen"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

【问题讨论】:

  • 你的 res/layout 目录中有welcome.xml 文件吗? R.java 自动生成成功了吗?
  • 如果welcome.xml 丢失,它将无法编译。我的猜测是它与startActivity(i); 有关
  • 我建议的第一件事是清理项目。根据错误,它在setContentView 崩溃,这表明它找不到布局。在 Eclipse 中,选择 Project->Clean,如果您没有将其设置为自动重新构建,则重新构建它。
  • 能否请您显示welcome.xml 布局?
  • 是的,我们需要查看welcome.xml

标签: java android exception nullpointerexception


【解决方案1】:

根据 LogCat 输出,问题出在您的 welcome.xml 布局文件中。由于某种原因,它充气不正确。

尝试删除android:textAppearance="?android:attr/textAppearanceLarge" 属性并确保您在启动前已在项目上运行Clean.. 命令。

另外确保 xml 文件中的所有引用都是有效的(即logov2img1)。我会暂时从您的 xml 文件中删除所有引用并检查它是否有帮助。

【讨论】:

  • 感谢您的所有建议。我已经自动清理并检查了它的建筑物,它是。上面的 xml 部分是welcome.xml,如果不清楚,请见谅。我也尝试过删除 android:textAppearance 位。
  • 尝试从welcome.xml 中删除所有引用并只留下一个带有硬编码文本的TextView(没有背景,没有ImageView,没有android:textAppearance 等等),看看它是否有效。
  • 再次为您提供帮助,我已尝试删除welcome.xml 并仅使用文本重新创建一个非常简单的页面,但这也不起作用,我想知道java是否搞砸了。跨度>
  • 确保在操作后得到完全相同的异常。我的意思是,你会看到这些堆栈跟踪:... Caused by: java.lang.NullPointerException at android.view.ViewGroup.addViewInner(ViewGroup.java:1860) at android.view.ViewGroup.addView(ViewGroup.java:1756) ...
【解决方案2】:

复制welcome.xml 文件并将其临时保存在任何地方,然后在删除文件后从eclipse 中删除此文件,再次粘贴您复制的welcome.xml 文件。然后再次运行。它肯定会运行您的应用程序。

【讨论】:

  • 上面的xml部分是welcome.xml。抱歉,不清楚。
  • 你按照我的建议做了吗???在你的 logcat 中的 PhoneWindow 消息是由于这个问题造成的。我和你一样遇到了同样的 Phonewindow 错误,然后我按照我的建议做了。所以请试试这个。
  • 感谢您的帮助,但没有奏效。这是一个非常奇怪的!
【解决方案3】:

我曾经也遇到过这个问题。然后我所做的就是从我的R.java 复制布局的id 并将其直接粘贴到setContentView()。即,在您的情况下,R.java->layout 中的欢迎 id。然后我的应用程序成功运行,没有任何异常。因此我意识到这只是一个暂时的问题。重新启动我的 Eclipse 后(有时您必须完全重新启动系统),我再次用 R.layout.welcome 替换了 id,它工作正常。 希望这会有所帮助...

【讨论】:

    【解决方案4】:

    您是否已将活动添加到清单中并添加了权限?

    请添加清单以便我向您推荐更多内容?

    您的崩溃可能是由于此权限被拒绝。 在 manifest.xml 中检查您的权限。

    【讨论】:

    • 这会给出不同的输出
    • 这将给出以下输出:android.content.ActivityNotFoundException: Unable to find explicit activity class {your_class_here}; have you declared this activity in your AndroidManifest.xml?
    • 你有没有检查日志 cat 中的权限被拒绝问题。我知道这个错误来了,但我给出了关于权限被拒绝的建议。每个人都知道这个错误,当你提到我的时候。好的跨度>
    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2020-04-27
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多