Java 新手-强制接近简单方法
现在添加了xml和logcat, 现在自定义查看代码,不幸的是我不在devel computer,所以我无法检查您的建议, @杰姆斯,我的自定义视图可能有错误的构造函数? @Falmari,我认为构建目标是2.2 希望有一件简单的事我不知道: 我有一个主要的游戏活动,这调用了一个我从view类扩展的类。 这是它的工作形式:Java 新手-强制接近简单方法,java,android,Java,Android,现在添加了xml和logcat, 现在自定义查看代码,不幸的是我不在devel computer,所以我无法检查您的建议, @杰姆斯,我的自定义视图可能有错误的构造函数? @Falmari,我认为构建目标是2.2 希望有一件简单的事我不知道: 我有一个主要的游戏活动,这调用了一个我从view类扩展的类。 这是它的工作形式: public class guappsXOMainGame extends Activity { private static final int cellsX=10; p
public class guappsXOMainGame extends Activity {
private static final int cellsX=10;
private static final int cellsY=10;
private guappsXOBoardView mBoardView;
@Override
public void onCreate(Bundle bund) {
super.onCreate(bund);
setContentView(R.layout.gamescreen);//loads xml layout called gamescreen
mBoardView = (guappsXOBoardView) findViewById(R.id.boardview);
}
一切都进行得很顺利,我的游戏屏幕显示得很好,直到我尝试了以下方法,这给了我力量:
public void onCreate(Bundle bund) {
super.onCreate(bund);
setContentView(R.layout.gamescreen);//loads xml layout called gamescreen
mBoardView = (guappsXOBoardView) findViewById(R.id.boardview);
mBoardView.hello();//problem line
}
我想要的是运行一个方法,从GuapsXobardView类中获取一些信息,这就是测试用例。
方法如下所示:
public void hello(){
int x = 1;
}
LogCat说:
01-29 20:50:46.415: INFO/ActivityManager(60): Starting activity: Intent { cmp=com.guapps/.guappsXOMainGame }
01-29 20:50:46.535: DEBUG/AndroidRuntime(564): Shutting down VM
01-29 20:50:46.535: WARN/dalvikvm(564): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): FATAL EXCEPTION: main
01-29 20:50:46.566: ERROR/AndroidRuntime(564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.guapps/com.guapps.guappsXOMainGame}: java.lang.NullPointerException
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.os.Looper.loop(Looper.java:123)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at java.lang.reflect.Method.invoke(Method.java:521)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at dalvik.system.NativeStart.main(Native Method)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): Caused by: java.lang.NullPointerException
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at com.guapps.guappsXOMainGame.onCreate(guappsXOMainGame.java:38)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-29 20:50:46.566: ERROR/AndroidRuntime(564): ... 11 more
01-29 20:50:46.596: WARN/ActivityManager(60): Force finishing activity com.guapps/.guappsXOMainGame
01-29 20:50:46.596: WARN/ActivityManager(60): Force finishing activity com.guapps/.guappsXOStart
01-29 20:50:46.745: DEBUG/dalvikvm(60): GC_FOR_MALLOC freed 7966 objects / 468848 bytes in 99ms
01-29 20:50:47.108: WARN/ActivityManager(60): Activity pause timeout for HistoryRecord{44009a08 com.guapps/.guappsXOMainGame}
01-29 20:50:48.375: INFO/Process(564): Sending signal. PID: 564 SIG: 9
01-29 20:50:48.385: INFO/ActivityManager(60): Process com.guapps (pid 564) has died.
01-29 20:50:48.385: INFO/WindowManager(60): WIN DEATH: Window{440530d0 com.guapps/com.guapps.guappsXOStart paused=true}
01-29 20:50:48.445: WARN/InputManagerService(60): Got RemoteException sending setActive(false) notification to pid 564 uid 10032
01-29 20:50:57.482: WARN/ActivityManager(60): Activity destroy timeout for HistoryRecord{43fc4248 com.guapps/.guappsXOStart}
01-29 20:50:57.488: WARN/ActivityManager(60): Activity destroy timeout for HistoryRecord{44009a08 com.guapps/.guappsXOMainGame}
以下是用于此的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<com.guapps.guappsXOBoardView
android:id="@+id/boardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
看起来很愚蠢,但是很多人都忘记了。。。是否已将活动添加到清单中?可能尝试从构造函数中删除requestFocus()。
你为什么还需要它?谢谢你的帮助,我找到了我的bug
自定义视图的构造函数具有super(上下文);而不是super(context,attrs),这会导致findviewbyd返回null。请发布完整的日志。也请发布gamescreen.xml文件。我认为错误就在这里,findViewById(R.id.boardview)结果返回null。您能发布自定义视图代码吗?如果缺少执行以下操作的构造函数:public guappsXOBoardView(上下文、属性集attrs、映射参数){super(上下文、属性集attrs、参数);},则该构造函数可能不起作用。您是根据哪个SDK版本生成的?match_parent是在2.3中引入的,我看不出工作onCreate方法和非工作onCreate方法之间有什么区别。它们应该是不同的吗?如果是这样的话,它将是一个不同的logcat消息。
package com.guapps;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class guappsXOBoardView extends View {
int screenHeight;
int screenWidth;
Context mContext;
private float leftG;
private float topG;
private float rightG;
private float botG;
Bitmap boardGrid;
public guappsXOBoardView(Context context, AttributeSet attrs) {
super(context);
requestFocus();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
screenWidth = View.MeasureSpec.getSize(widthMeasureSpec);
screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(screenWidth, screenHeight);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.WHITE);
}
.....