我正在制作一个应用程序应该是一个非常简单的街头霸王版本,实际上只需轻扫一下即可,AI可以踢或打,然后健康就会被取消。我只是试图让健康部分失效,但每次我刷程序退出,我的TextView得到一个空指针异常。起初我认为问题是我在一个没有使用setContentView方法的类中使用了findViewById,但是当我将它移动到MainActivity类时,我仍然遇到同样的错误。与此主题相关的其他问题已经说过setContentView方法指向不同的布局,但是这个确定指向正确的布局。这是GameView类,MainActivity类和activity_main xml文件。
GameView:
package com.example.images;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
@SuppressWarnings({"unused" })
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private GameThread thread;
private Animation a;
private EnemyAnim ea;
private int count = 0;
private float down, up;
private Character ryu, chun_li;
private TextView r,l;
public GameView(Context context) {
super(context);
up = 0;
down = 0;
getHolder().addCallback(this);
thread = new GameThread(getHolder(), this, context);
// animation = new Animation(100,100);
a = new Animation(context);
ea = new EnemyAnim(context);
ryu = new Character(1000);
chun_li = new Character(1000);
r = MainActivity.r;
l = MainActivity.l;
setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
String attack;
double action = Math.random();
if (action >= .5) {
attack = "kick";
}
else {
attack = "punch";
}
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
down = event.getY();
case MotionEvent.ACTION_UP:
up = event.getY();
}
if (down < up && Math.abs(down-up)>=20) {
a.setAnimation("punch");
chun_li.act("punch");
}
else if (down > up && Math.abs(down-up)>=20) {
a.setAnimation("kick");
chun_li.act("kick");
}
ea.setAnimation(attack);
ryu.act(attack);
r.setText(ryu.getHealth());
l.setText(chun_li.getHealth());
return true;
}
@Override
public void onDraw(Canvas canvas) {
try {
canvas.drawColor(Color.BLACK);
a.getCurrentFrame().draw(canvas);
ea.getCurrentFrame().draw(canvas);
}
catch (NullPointerException e) {
return;
}
//count++;
}
}
MainActivity:
package com.example.images;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
@SuppressWarnings("all")
public class MainActivity extends FragmentActivity {
public static TextView r,l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = (TextView)findViewById(R.id.ryu_health);
l = (TextView)findViewById(R.id.li_health);
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.images.MainActivity" >
<TextView
android:id="@+id/ryu_health"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Ryu health: 1000"
android:gravity="left|center"
android:textColor="@android:color/black" />
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.example.images.FragmentThing" />
<TextView
android:id="@+id/li_health"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="right|center"
android:text="Chun-Li health: 1000"
android:textColor="@android:color/black" />
</RelativeLayout>
错误日志:
08-13 11:05:52.671: E/AndroidRuntime(7997): FATAL EXCEPTION: main
08-13 11:05:52.671: E/AndroidRuntime(7997): Process: com.example.images, PID: 7997
08-13 11:05:52.671: E/AndroidRuntime(7997): java.lang.NullPointerException
08-13 11:05:52.671: E/AndroidRuntime(7997): at com.example.images.GameView.onTouchEvent(GameView.java:84)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.View.dispatchTouchEvent(View.java:8107)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2101)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2101)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2101)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2101)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2101)
08-13 11:05:52.671: E/AndroidRuntime(7997): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2280)
08-13 11:05:52.671: E/AndroidRuntime(7997): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1615)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.app.Activity.dispatchTouchEvent(Activity.java:2544)
08-13 11:05:52.671: E/AndroidRuntime(7997): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.View.dispatchPointerEvent(View.java:8315)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4597)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4465)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4023)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4077)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4046)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4157)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4054)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4214)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4023)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4077)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4046)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4054)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4023)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6404)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6308)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6279)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6244)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6484)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.os.MessageQueue.nativePollOnce(Native Method)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.os.MessageQueue.next(MessageQueue.java:138)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.os.Looper.loop(Looper.java:123)
08-13 11:05:52.671: E/AndroidRuntime(7997): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-13 11:05:52.671: E/AndroidRuntime(7997): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 11:05:52.671: E/AndroidRuntime(7997): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 11:05:52.671: E/AndroidRuntime(7997): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-13 11:05:52.671: E/AndroidRuntime(7997): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-13 11:05:52.671: E/AndroidRuntime(7997): at dalvik.system.NativeStart.main(Native Method)