Android 开发:“线程因未捕获的异常而退出”

2023-12-30

我正在尝试创建我的第一个 Android 应用程序(游戏),但开始时遇到一些困难。

当我运行我的代码时,我收到以下错误日志:

05-25 02:41:51.022: WARN/dalvikvm(634): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-25 02:41:51.040: ERROR/AndroidRuntime(634): FATAL EXCEPTION: main
05-25 02:41:51.040: ERROR/AndroidRuntime(634): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stickfigs.nmg/com.stickfigs.nmg.NMG}: java.lang.NullPointerException
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.os.Looper.loop(Looper.java:123)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at java.lang.reflect.Method.invoke(Method.java:521)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at dalvik.system.NativeStart.main(Native Method)
05-25 02:41:51.040: ERROR/AndroidRuntime(634): Caused by: java.lang.NullPointerException
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at com.stickfigs.nmg.NMG.onCreate(NMG.java:32)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-25 02:41:51.040: ERROR/AndroidRuntime(634):     ... 11 more
05-25 02:41:51.062: WARN/ActivityManager(59):   Force finishing activity com.stickfigs.nmg/.NMG

我认为问题在于“线程因未捕获的异常而退出”部分,我不知道异常可能是什么或导致它的原因。

这是我的代码:

NMGView.java:com.stickfigs.NMG 包;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class NMGView extends SurfaceView implements SurfaceHolder.Callback {

    class NMGThread extends Thread {
        //State-tracking constants
        public static final int STATE_LOSE = 1;
        public static final int STATE_PAUSE = 2;
        public static final int STATE_READY = 3;
        public static final int STATE_RUNNING = 4;
        public static final int STATE_WIN = 5;

        /** The state of the game. One of READY, RUNNING, PAUSE, LOSE, or WIN */
        private int mode;

        /** Handle to the surface manager object we interact with */
        private SurfaceHolder surfaceHolder;

        public NMGThread(SurfaceHolder surfaceHolderc, Context contextc) {
            // get handles to some important objects
            surfaceHolder = surfaceHolderc;
            context = contextc;

        }

        /**
         * Restores game state from the indicated Bundle. Typically called when
         * the Activity is being restored after having been previously
         * destroyed.
         * 
         * @param savedState Bundle containing the game state
         */
        public synchronized void restoreState(Bundle savedState) {
            synchronized (surfaceHolder) {
                setState(STATE_PAUSE);
                }
        }

        /**
         * Sets the game mode. That is, whether we are running, paused, in the
         * failure state, in the victory state, etc.
         * 
         * @param mode one of the STATE_* constants
         * @param message string to add to screen or null
         */
        public void setState(int modec) {
            synchronized (surfaceHolder) {
                mode = modec;
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    /** Handle to the application context, used to e.g. fetch Drawables. */
    private Context context;

    /** The thread that actually draws the animation */
    private NMGThread thread;

    public NMGView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // register our interest in hearing about changes to our surface
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        // create thread only; it's started in surfaceCreated()
        thread = new NMGThread(holder, context);

        setFocusable(true); // make sure we get key events
    }

    /**
     * Fetches the animation thread corresponding to this LunarView.
     * 
     * @return the animation thread
     */
    public NMGThread getThread() {
        return thread;
    }
}

NMG.java:

package com.stickfigs.nmg;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.stickfigs.nmg.NMGView.NMGThread;

public class NMG extends Activity {
    /** Called when the activity is first created. */

    /** A handle to the thread that's actually running the animation. */
    private NMGThread nMGThread;

    /** A handle to the View in which the game is running. */
    private NMGView nMGView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Turn off the window's title bar
        // TODO Turn off the status bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // tell system to use the layout defined in our XML file
        setContentView(R.layout.nmg_layout);

        // get handles to the LunarView from XML, and its LunarThread
        nMGView = (NMGView) findViewById(R.id.nmg);
        nMGThread = nMGView.getThread();

        if (savedInstanceState == null) {
            // we were just launched: set up a new game
            nMGThread.setState(NMGThread.STATE_READY);
            Log.w(this.getClass().getName(), "SIS is null");
        } else {
            // we are being restored: resume a previous game
            nMGThread.restoreState(savedInstanceState);
            Log.w(this.getClass().getName(), "SIS is nonnull");
        }
    }
}

更新:这是我的 R.java 和 nmg_layout.xml:

R.java:com.stickfigs.nmg 包;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int nmg=0x7f050000;
    }
    public static final class layout {
        public static final int nmg_layout=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

nmg_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.stickfigs.nmg.NMGView
      android:id="@+id/nmg"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>
</FrameLayout>

如果您查看堆栈跟踪,您将看到一行“由...引起”(有时不止一行)。其中最后一项是重要的。它说NMG.java的第32行出现空指针异常。该行及其之前的行是:

nMGView = (NMGView) findViewById(R.id.nmg);
nMGThread = nMGView.getThread();

显然,没有带有id的视图R.id.nmg正在布局中R.layout.nmg_layout。这就是导致你的问题的原因。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 开发:“线程因未捕获的异常而退出” 的相关文章

随机推荐

  • 约束必须是字符串(或 null)

    我找不到错误来挽救我的生命 错误是 约束必须是字符串 或空 我不知道为什么它会给我这个错误 我必须错过一些简单的东西 我尝试添加 例如 dataPane new JPanel new GridBagLayout 到我所有的面板 什么也没有
  • Scala 中的阻塞关键字

    有什么区别Future blocking blockingCall and blocking Future blockingCall 这两个都定义在scala concurrent 我看过在 scala 文档中 http docs scal
  • 泛型和从不同类调用重载方法 - 优先级问题[重复]

    这个问题在这里已经有答案了 首先 对这个标题感到抱歉 但我想不出更好的了 我的问题可以通过简单的代码示例来呈现 public static class Test
  • mysql - 如何处理使用特殊字符%(百分比)和_(下划线)的查询搜索

    我的数据库中有 2 条记录 Tom Jerry and Ninja Kids 一个有特殊字符 百分比 另一个有 下划线 我在 mysql 终端尝试了以下命令 SELECT FROM CUSTOMER WHERE NAME LIKE SELE
  • 使用 dart 和 flutter 与 google calendar api 来获取用户日历上的事件列表

    我正在尝试通过重新构建我之前用 Java 编写的应用程序来学习如何使用 dart 和 flutter 其中涉及使用 Google 自己的日历 API 从 Google 日历获取事件 通过阅读 不是很详细 googleapis auth 包的
  • Xpath:从 id 属性与 id 锚点的匹配中查找元素值

    我想找到与 id 属性匹配的元素的值 我只有 ref 带 的位 锚点 我正在寻找 partyId 的值 lt party id partyA gt lt partyId gt THEID lt partyId gt 但要到达那里 我只有以下
  • 如何将受信任的证书添加到 OkHttp

    我需要使用 OkHttp 客户端信任一个特定站点的证书 我在这里找到了解决方案 https jebware com blog p 340 https jebware com blog p 340 这段代码与我想要信任的服务器配合得很好 唯一
  • Android 外部存储与 SD 卡

    阅读有关存储文件的 Android 文档后 我发现外部存储可以包括可移动 SD 卡和设备内部存储 即不可移动 选择将文件保存到外部存储时 是否可以区分可移动存储和不可移动存储 我认为您无法可靠地区分内部和外部 SD 存储 乍一看 您似乎可以
  • 如何在imageView上显示从内部存储中选择的图像?

    我是在 android 中处理图像的新手 我想从内部存储加载图像 但它给了我权限被拒绝错误 然后我已将权限添加到 android 清单文件 但我仍然无法完成我的任务 这是我的代码 import android graphics Bitmap
  • 将Google Data Studio连接到本地Mysql服务器

    我已经开始使用 Google 的 Data Studio 我发现只需很少或根本不需要编码技能 就可以轻松地将 Excel 数据转换为直观的业务仪表板 但我这里有一个问题 每当我尝试连接到 Mysql DB 在我的本地系统上运行 时 我都会遇
  • 在 C# winform 中如何识别应用程序显示在哪个监视器上

    重复的如何在 C 中找到应用程序正在哪个屏幕上运行 https stackoverflow com questions 549751 how do i find what screen the application is running
  • 应用程序无法搭建项目

    我在 VS 2013 Professional 中创建了一个 MVC 5 应用程序 然后首先将 EF 6 1 代码与 SQL Server Express 上的现有数据库一起使用 当我尝试创建视图时 我使用 新脚手架项目 然后选择 带有视图
  • 安卓内部存储

    我需要在手机内存中存储一 个对象 如何在 Android 中执行此操作 希望您觉得这个博客有用 http pierrchen blogspot com 2011 11 android storage html http pierrchen
  • 在 Matlab 用户界面中跨多个 m 文件搜索文本

    有办法吗withinmatlab 用户界面在多个 m 文件中搜索某些文本 按 Ctrl Shift f 或转到菜单 编辑 gt 查找文件 您将得到一个很好的对话框 希望它能够满足您的要求
  • .htaccess 错误 - ERR_TOO_MANY_REDIRECTS

    我有这个 htaccess 文件要重定向http to https 我也做了www 到根域重定向 www 到根域有效 然而https 重定向没有 如果我设置RewriteCond HTTPS on to RewriteCond HTTPS
  • MongoDB bind_ip 错误:bind() 失败 errno:99 无法为套接字分配请求的地址

    我想配置 mongodb 以允许来自外部 IP 地址的远程连接 例如66 31 123 123 Setting 0 0 0 0 to bind ip可行 但我想更加严格 只允许某些 IP 地址进行连接 我附加了66 31 123 123 t
  • 如何在 asp.net 会话变量过期之前执行服务器端代码?

    在我的 asp net 网站中 我在用户登录时创建一个会话 我想在该会话到期之前在数据库中执行一些操作 我在确定应该在哪里编写代码以及如何知道会话时遇到问题即将过期 我不确定 Global asax 的 session end 事件是否适合
  • 如何访问第二个麦克风 Android(例如 Galaxy 3)

    现在很多智能手机都有不止一个麦克风 一个用于语音输入 另一个用于减少环境噪音 我想知道如何独立访问两个麦克风的信号 或者关闭其中一个麦克风 欢迎任何想法或评论 多谢 我对 Galaxy S3 不太熟悉 但以下情况适用于我使用过的大多数设备
  • strtotime 结果没有意义,php bug?

    以下行 echo date d strtotime First Saturday August 2015 prints 08 这似乎没有任何意义 因为一周中的某一天第一次出现不能在 7 号之后 这是一个 php bug 还是一个 php b
  • Android 开发:“线程因未捕获的异常而退出”

    我正在尝试创建我的第一个 Android 应用程序 游戏 但开始时遇到一些困难 当我运行我的代码时 我收到以下错误日志 05 25 02 41 51 022 WARN dalvikvm 634 threadid 1 thread exiti