Android的发展:"螺纹与未捕获的异常&QUOT退出; [英] Android Development: "thread exiting with uncaught exception"
问题描述
我想创造我的第一个Android应用程序(游戏),但我有一些困难的开始。
当我运行我的code我得到这个错误日志:
二月五日至25日:41:51.022:WARN / dalvikvm(634):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):致命异常:主要
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.stickfigs.nmg / com.stickfigs.nmg.NMG}:显示java.lang.NullPointerException
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.os.Handler.dispatchMessage(Handler.java:99)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.os.Looper.loop(Looper.java:123)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.ActivityThread.main(ActivityThread.java:4627)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在java.lang.reflect.Method.invokeNative(本机方法)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在java.lang.reflect.Method.invoke(Method.java:521)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在dalvik.system.NativeStart.main(本机方法)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):由:显示java.lang.NullPointerException
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在com.stickfigs.nmg.NMG.onCreate(NMG.java:32)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
2月5日至二十五日:41:51.040:ERROR / AndroidRuntime(634):11 ...更多
2月5日至二十五日:41:51.062:WARN / ActivityManager(59):强制整理活动com.stickfigs.nmg / .NMG
我认为这个问题是这样的线程与未捕获的异常退出的一部分,我不知道是什么的例外可能是或什么导致它。
下面是我的code:
NMGView.java: 包com.stickfigs.NMG;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.AttributeSet;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
类NMGView扩展了SurfaceView实现SurfaceHolder.Callback {
类NMGThread继承Thread {
//状态跟踪常数
公共静态最终诠释STATE_LOSE = 1;
公共静态最终诠释STATE_PAUSE = 2;
公共静态最终诠释STATE_READY = 3;
公共静态最终诠释STATE_RUNNING = 4;
公共静态最终诠释STATE_WIN = 5;
/ **游戏的状态。一位准备,跑,暂停,输或赢* /
私人诠释方式;
/ **句柄,我们与之交互的面管理器对象* /
私人SurfaceHolder surfaceHolder;
公共NMGThread(SurfaceHolder surfaceHolderc,上下文contextc){
//获取句柄一些重要对象
surfaceHolder = surfaceHolderc;
上下文= contextc;
}
/ **
*恢复从指示捆绑游戏状态。通常情况下调用时
*该项活动已经previously后恢复
*销毁。
*
*参数savedState包包含了比赛状态
* /
市民同步无效restoreState(包savedState){
同步(surfaceHolder){
的setState(STATE_PAUSE);
}
}
/ **
*设置游戏模式。也就是说,我们是否正在运行,暂停,在
*故障状态,在胜利的状态,等等。
*
* @参数模式STATE_之一*常量
* @参数消息字符串添加到屏幕或空
* /
公共无效的setState(INT MODEC){
同步(surfaceHolder){
模式= MODEC;
}
}
}
@覆盖
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
INT高度){
// TODO自动生成方法存根
}
@覆盖
公共无效surfaceCreated(SurfaceHolder持有者){
// TODO自动生成方法存根
}
@覆盖
公共无效surfaceDestroyed(SurfaceHolder持有者){
// TODO自动生成方法存根
}
/ **手柄的应用程序上下文,用于如获取可绘制。 * /
私人上下文的背景下;
/ **实际绘制动画*线程/
私人NMGThread线;
公共NMGView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
//注册在听到关于改变我们的表面我们的利益
SurfaceHolder支架= getHolder();
holder.addCallback(本);
//只有创建线程;它开始于surfaceCreated()
线程=新NMGThread(架,背景);
setFocusable(真正的); //确保我们获得的重要事件
}
/ **
*获取相应于该LunarView动画线程。
*
返回:动画线程
* /
公共NMGThread getThread(){
返回线程;
}
}
NMG.java:
包com.stickfigs.nmg;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Window;
进口com.stickfigs.nmg.NMGView.NMGThread;
公共类NMG延伸活动{
/ **第一次创建活动时调用。 * /
/ **的句柄实际上运行该动画的线程。 * /
私人NMGThread nMGThread;
/ **手柄在其中游戏运行视图。 * /
私人NMGView nMGView;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//关闭窗口的标题栏
// TODO关闭状态栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//告诉系统来使用我们的XML文件中定义的布局
的setContentView(R.layout.nmg_layout);
//获取句柄LunarView从XML,它LunarThread
nMGView =(NMGView)findViewById(R.id.nmg);
nMGThread = nMGView.getThread();
如果(savedInstanceState == NULL){
//我们刚刚推出:成立一个新的游戏
nMGThread.setState(NMGThread.STATE_READY);
Log.w(this.getClass()的getName(),SIS为空);
} 其他 {
//我们正在恢复:恢复previous游戏
nMGThread.restoreState(savedInstanceState);
Log.w(this.getClass()的getName(),SIS为非空);
}
}
}
更新:这是我的R.java和nmg_layout.xml:
R.java: 包com.stickfigs.nmg;
公共final类环R {
公共静态final类ATTR {
}
公共静态final类绘制{
公共静态最终诠释图标= 0x7f020000;
}
公共静态最后的类ID {
公共静态最终诠释NMG = 0x7f050000;
}
公共静态最后的客舱布局{
公共静态最终诠释nmg_layout = 0x7f030000;
}
公共静态final类的字符串{
公共静态最终诠释APP_NAME = 0x7f040001;
公共静态最终诠释你好= 0x7f040000;
}
}
nmg_layout.xml:
< XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< com.stickfigs.nmg.NMGView
机器人:ID =@ + ID / NMG
机器人:layout_width =FILL_PARENT
机器人: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
。那是什么导致你的问题。
I'm trying to create my first Android App (a game) but I'm having some difficulties getting started.
When I run my code I get this error log:
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
I think the problem is this "thread exiting with uncaught exception" part, I have no idea what the exception could be or what's causing it.
Here is my code:
NMGView.java: package 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");
}
}
}
UPDATE: Here is my R.java and nmg_layout.xml:
R.java: package 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>
If you look through the stack trace, you will see a "Caused by..." line (sometimes more than one). The last one of those is the important one. It says that there was a null pointer exception on line 32 of NMG.java. That line, and the line before it, are:
nMGView = (NMGView) findViewById(R.id.nmg);
nMGThread = nMGView.getThread();
Evidently, no view with id R.id.nmg
is in layout R.layout.nmg_layout
. That's what's causing your problem.
这篇关于Android的发展:&QUOT;螺纹与未捕获的异常&QUOT退出;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!