我正在开发一个应用程序,您可以在其中触摸屏幕并让篮球移动到那里。我几乎就在那里,除了当你从活动列表中打开这个迷你应用程序时它会强制关闭。扫描了几次后,我找不到任何东西。
这是我的 Java 类,之后是 Logcat:
package com.frostbytedev.addsub;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class SurfaceViewExample extends Activity implements OnTouchListener {
OurView v;
Bitmap ball;
float x = 0;
float y = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = new OurView(this);
setContentView(v);
v.setOnTouchListener(this);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.bball);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isItOkay = false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isItOkay) {
if(!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 150, 150, 150);
c.drawBitmap(ball, x-(ball.getWidth()/2), y-(ball.getHeight()/2), null);
holder.unlockCanvasAndPost(c);
}
}
public void pause(){
isItOkay = false;
while(true){
try{
t.join();
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void resume() {
isItOkay = true;
t = new Thread(this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
return false;
}
}
日志猫:
05-09 19:24:35.730: D/libEGL(8218): loaded /system/lib/egl/libEGL_adreno200.so
05-09 19:24:35.740: D/libEGL(8218): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-09 19:24:35.750: D/libEGL(8218): loaded /system/lib/egl/libGLESv2_adreno200.so
05-09 19:24:35.800: D/OpenGLRenderer(8218): Enabling debug mode 0
05-09 19:24:44.658: D/AndroidRuntime(8218): Shutting down VM
05-09 19:24:44.658: W/dalvikvm(8218): threadid=1: thread exiting with uncaught exception (group=0x40a7a930)
05-09 19:24:44.668: E/AndroidRuntime(8218): FATAL EXCEPTION: main
05-09 19:24:44.668: E/AndroidRuntime(8218): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
05-09 19:24:44.668: E/AndroidRuntime(8218): at com.frostbytedev.addsub.Menu.onListItemClick(Menu.java:25)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.widget.AbsListView.performItemClick(AbsListView.java:1102)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2751)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.widget.AbsListView$1.run(AbsListView.java:3426)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.os.Handler.handleCallback(Handler.java:725)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.os.Looper.loop(Looper.java:137)
05-09 19:24:44.668: E/AndroidRuntime(8218): at android.app.ActivityThread.main(ActivityThread.java:5237)
05-09 19:24:44.668: E/AndroidRuntime(8218): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:24:44.668: E/AndroidRuntime(8218): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 19:24:44.668: E/AndroidRuntime(8218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
05-09 19:24:44.668: E/AndroidRuntime(8218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
05-09 19:24:44.668: E/AndroidRuntime(8218): at dalvik.system.NativeStart.main(Native Method)