找不到我的代码中的错误

时间:2012-03-03 00:25:29

标签: android

我正在尝试在android上创建一个游戏应用程序,我正在使用类文件设计用户界面。但我似乎得到了异常的nullpointerexception。我无法找到bug的位置。这是GAME.CLASS代码。

package android.hello;
import android.app.Activity;
import android.os.Bundle;
public class game extends Activity 
{
public static final String KEY_PLAYERS="android.hello.new_game";
private static final String TAG="Serpentes E Escades";
public static final int ONE_PLAYER=1;
public static final int TWO_PLAYER=2;
private int puzzle[];
private PuzzleView puzzleView;
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(puzzleView);
    int players=getIntent().getIntExtra(KEY_PLAYERS,ONE_PLAYER);
    puzzleView.requestFocus();
}
}

这是游戏文件的视图

package android.hello;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
public class PuzzleView extends View
{
private final game game1;
private static final String TAG="SEE";
public PuzzleView(Context context)
{
    super(context);
    this.game1=(game)context;
    setFocusable(true);
    setFocusableInTouchMode(true);
}
private float width;
private float height;
private int selX;
private int selY;
private final Rect selRect=new Rect();
protected void onSizeChanged(int w,int h,int oldw,int oldh)
{
    width=w/10;
    height=h/10;
    getRect(selX,selY,selRect);
    super.onSizeChanged(w, h, oldw, oldh);
}
private void getRect(int x,int y,Rect rect)
{
    rect.set((int)(x*width),(int)(y*height),(int)(x*width+width),(int)(y*height+height));
}
public void onDraw(Canvas canvas)
{

    try
    {
        Paint background=new Paint();
        background.setColor(getResources().getColor(R.color.background));
        canvas.drawRect(0,0,getWidth(),getHeight(),background);
    }
    catch (NullPointerException e) 
    {
        Log.d("MYTAG",e.toString());
    }
}
}

图片1:http://s17.postimage.org/v1tqi7si7/image.jpg 图2:http://s18.postimage.org/92ly8vwux/image.jpg

点击one_player后,强制关闭错误

我认为这个bug会出现在puzzleview中,因为当我尝试将游戏类的内容视图设置为普通的xml时,它确实有效。

这是我的日志

02-27 21:25:42.199: W/dalvikvm(1932): threadid=1: thread exiting with uncaught     exception (group=0x40015560)
02-27 21:25:42.316: E/AndroidRuntime(1932): FATAL EXCEPTION: main
02-27 21:25:42.316: E/AndroidRuntime(1932): android.content.ActivityNotFoundException:     Unable to find explicit activity class {android.hello/android.hello.prefs}; have you     declared this activity in your AndroidManifest.xml?
02-27 22:10:57.670: W/dalvikvm(2101): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
02-27 22:10:57.911: E/AndroidRuntime(2101): FATAL EXCEPTION: main
02-27 22:10:57.911: E/AndroidRuntime(2101): android.content.ActivityNotFoundException:     Unable to find explicit activity class {android.hello/android.hello.game}; have you     declared this activity in your AndroidManifest.xml?
02-27 22:12:43.270: W/dalvikvm(2135): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-27 22:12:43.330: E/AndroidRuntime(2135): FATAL EXCEPTION: main
02-27 22:12:43.330: E/AndroidRuntime(2135): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-27 22:12:43.330: E/AndroidRuntime(2135): Caused by: java.lang.NullPointerException
02-27 22:15:19.280: W/dalvikvm(2202): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-27 22:15:19.310: E/AndroidRuntime(2202): FATAL EXCEPTION: main
02-27 22:15:19.310: E/AndroidRuntime(2202): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-27 22:15:19.310: E/AndroidRuntime(2202): Caused by: java.lang.NullPointerException
02-27 22:27:41.699: W/dalvikvm(2283): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-27 22:27:41.880: E/AndroidRuntime(2283): FATAL EXCEPTION: main
02-27 22:27:41.880: E/AndroidRuntime(2283): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-27 22:27:41.880: E/AndroidRuntime(2283): Caused by: java.lang.NullPointerException
02-28 22:34:15.789: W/dalvikvm(2345): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:34:16.009: E/AndroidRuntime(2345): FATAL EXCEPTION: main
02-28 22:34:16.009: E/AndroidRuntime(2345): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:34:16.009: E/AndroidRuntime(2345): Caused by: java.lang.NullPointerException
02-28 22:37:19.782: W/dalvikvm(2430): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:37:19.879: E/AndroidRuntime(2430): FATAL EXCEPTION: main
02-28 22:37:19.879: E/AndroidRuntime(2430): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:37:19.879: E/AndroidRuntime(2430): Caused by: java.lang.NullPointerException
02-28 22:41:41.050: W/dalvikvm(2491): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:41:41.089: E/AndroidRuntime(2491): FATAL EXCEPTION: main
02-28 22:41:41.089: E/AndroidRuntime(2491): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:41:41.089: E/AndroidRuntime(2491): Caused by: java.lang.NullPointerException
02-28 22:43:07.100: W/dalvikvm(2558): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:43:07.160: E/AndroidRuntime(2558): FATAL EXCEPTION: main
02-28 22:43:07.160: E/AndroidRuntime(2558): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:43:07.160: E/AndroidRuntime(2558): Caused by: java.lang.NullPointerException
02-28 22:49:30.359: W/dalvikvm(2592): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:49:30.409: E/AndroidRuntime(2592): FATAL EXCEPTION: main
02-28 22:49:30.409: E/AndroidRuntime(2592): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:49:30.409: E/AndroidRuntime(2592): Caused by: java.lang.NullPointerException
02-28 22:51:25.419: W/dalvikvm(2627): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:51:25.479: E/AndroidRuntime(2627): FATAL EXCEPTION: main
02-28 22:51:25.479: E/AndroidRuntime(2627): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:51:25.479: E/AndroidRuntime(2627): Caused by: java.lang.NullPointerException
02-28 22:52:12.010: W/dalvikvm(2694): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 22:52:12.090: E/AndroidRuntime(2694): FATAL EXCEPTION: main
02-28 22:52:12.090: E/AndroidRuntime(2694): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 22:52:12.090: E/AndroidRuntime(2694): Caused by: java.lang.NullPointerException
02-28 23:15:45.530: W/dalvikvm(2729): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 23:15:45.690: E/AndroidRuntime(2729): FATAL EXCEPTION: main
02-28 23:15:45.690: E/AndroidRuntime(2729): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 23:15:45.690: E/AndroidRuntime(2729): Caused by: java.lang.NullPointerException
02-28 23:17:27.029: W/dalvikvm(2783): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 23:17:27.139: E/AndroidRuntime(2783): FATAL EXCEPTION: main
02-28 23:17:27.139: E/AndroidRuntime(2783): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 23:17:27.139: E/AndroidRuntime(2783): Caused by: java.lang.NullPointerException
02-28 23:19:42.229: W/dalvikvm(2817): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 23:19:42.299: E/AndroidRuntime(2817): FATAL EXCEPTION: main
02-28 23:19:42.299: E/AndroidRuntime(2817): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 23:19:42.299: E/AndroidRuntime(2817): Caused by: java.lang.NullPointerException
02-28 23:21:00.949: W/dalvikvm(2876): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 23:21:01.059: E/AndroidRuntime(2876): FATAL EXCEPTION: main
02-28 23:21:01.059: E/AndroidRuntime(2876): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 23:21:01.059: E/AndroidRuntime(2876): Caused by: java.lang.NullPointerException
02-28 23:32:44.281: W/dalvikvm(2914): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 23:32:44.350: E/AndroidRuntime(2914): FATAL EXCEPTION: main
02-28 23:32:44.350: E/AndroidRuntime(2914): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.hello/android.hello.game}: java.lang.NullPointerException
02-28 23:32:44.350: E/AndroidRuntime(2914): Caused by: java.lang.NullPointerException

由于我无法发布整个日志或额外的超链接,我将链接粘贴在评论中。

0 个答案:

没有答案