Android开发:“线程退出且未捕获异常”

如何解决Android开发:“线程退出且未捕获异常”

| 我正在尝试创建我的第一个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>
    

解决方法

如果查看堆栈跟踪,将看到\“ Caused by ... \”行(有时不止一个)。其中的最后一个是重要的。它说NMG.java的第32行有一个空指针异常。该行及其前面的行是:
nMGView = (NMGView) findViewById(R.id.nmg);
nMGThread = nMGView.getThread();
显然,布局
R.layout.nmg_layout
中没有ID为
R.id.nmg
的视图。这就是导致您出现问题的原因。     ,在您的XML文件中,确实存在
R.id.nmg
。 因此,我认为问题是由资源中的“ 9”对象膨胀引起的。 您应该检查
NMGView
源代码,尤其是在其构造函数中。     ,如果您在\“ build.gradle(Module:app)\”中使用\“ multiDexEnabled true \”,请从\“ defaultConfig \”中删除此行并同步项目。 在我的情况下有效!!!     ,发生此问题的原因是,在大多数情况下,销毁surfaceView时,SurfaceView onDraw()方法正在运行,然后会出现NULL POINTER ERROR,这是因为画布当时不存在。我已经解决了使用NullPointerException捕获所有绘图内容的问题:
@Override
public void onDraw(Canvas canvas) {

    try {
         //Drawing Stuff

   }catch(NullPointerException e){
        Log.e(\"NULL POINTER EXCEPTION\",\"Canvas NULL POINTER\");
    }
}
如果在Main活动中实现onPause()和onDestroy()方法,则将获得以下顺序:首先:E / onPause:暂停->下一个E / surfaceDestroyed:表面破坏-> E / NULL POINTER EXCEPTION:画布NULL POINTER->最终 E / onDestroy:毁灭 这是我正在使用的surfaceDestroyed方法:
   @Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    Log.e(\"surfaceDestroyed\",\"SURFACE DESTROYED \");


    thread.setRunning(false);
        try {
            //thread.setRunning(false);
            thread.join();
            } catch (InterruptedException e) {
           Log.e(\"Surface Thread Stopped\",\"SURFACE THREAD STOPPED\");
        }

}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-