如何在 Android 应用程序中调试 NullPointerException?

发布于 2025-01-03 15:31:47 字数 6731 浏览 2 评论 0原文

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

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>

I have a very simple app which isn't starting correctly. The app should load a splashscreen, and then the main app. For some reason it's now not working -

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 for the splashscreen (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();
    }
}

Manifest file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿! 2025-01-10 15:31:47

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

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

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

According to the LogCat output the problem is in your welcome.xml layout file. It is inflated incorrectly for some reason.

Try removing android:textAppearance="?android:attr/textAppearanceLarge" attribute and ENSURE that you have run Clean.. command on your project before the launch.

Also ENSURE that all the references in the xml-file are valid (i.e. logov2, img1). I would temporarily remove all references from your xml-file and check if it helps.

青瓷清茶倾城歌 2025-01-10 15:31:47

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

Copy the welcome.xml file and save it anywhere for temporary time then delete this file from the eclipse after delete the file again paste that welcome.xml file which you copied.Then run again.It will sure run your app.

很糊涂小朋友 2025-01-10 15:31:47

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

I was also suffering from this problem once. Then what i did was i just copied the id of the layout from my R.java and pasted it directly to setContentView(). ie, Here in your case the id of welcome in R.java->layout. then my app ran successfully without any exception. Thus i realized that its just a temporary problem. After restarting my eclipse (sometimes you have to restart your system fully) I replaced the id again by R.layout.welcome and it worked fine.
Hope this helps...

流年已逝 2025-01-10 15:31:47

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

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

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

Have you added the activity in to the manifest and also added the permission?

Please add the manifest so that I can refer you something more?

You have crash may be due to this Permission denied.
Check your permission in the manifest.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文