我在NullPointerException
下面找到了if (mBubbleView.intersects(tabPosX, tabPosY))
,我不知道如何修复它,你能帮忙吗?感谢。
公共类BubbleActivity扩展了Activity {
private static final String TAG = "Lab-Graphics";
// The Main view
private RelativeLayout mFrame;
// Bubble image's bitmap
private Bitmap mBitmap;
// Display dimensions
private int mDisplayWidth, mDisplayHeight;
// Gesture Detector
private GestureDetector mGestureDetector;
//BubbleView
private BubbleView mBubbleView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up user interface
mFrame = (RelativeLayout) findViewById(R.id.frame);
// Load basic bubble Bitmap
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b64);
Log.i(TAG, "Before setting up Gesture Detector");
//Setup Gesture Detector
setupGestureDetector();
Log.i(TAG, "After setting up Gesture Detector");
}
// Set up GestureDetector
private void setupGestureDetector() {
mGestureDetector = new GestureDetector(this,
new GestureDetector.SimpleOnGestureListener() {
// If a single tap intersects a BubbleView, then pop the BubbleView
// Otherwise, create a new BubbleView at the tap's location and add
// it to mFrame. You can get all views from mFrame with ViewGroup.getChildAt()
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
// TODO - Implement onSingleTapConfirmed actions.
// You can get all Views in mFrame using the
// ViewGroup.getChildCount() method
int pointerIndex = event.getActionIndex();
float tabPosX = event.getX(pointerIndex);
float tabPosY = event.getY(pointerIndex);
mBubbleView = (BubbleView) mFrame.getChildAt(pointerIndex);
Log.i(TAG, "pointerIndex: " + pointerIndex + " mFrame.getChildCount(): " + mFrame.getChildCount() + " mFrame.getChildAt(pointerIndex): " + mFrame.getChildAt(pointerIndex));
Log.i(TAG, "mBubbleView is initiated:" + mBubbleView);
if(mBubbleView.intersects(tabPosX, tabPosY)){
Log.i(TAG, "If mBubbleView.intersects(tabPosX, tabPosY) = TRUE");
}else{
Log.i(TAG, "If mBubbleView.intersects(tabPosX, tabPosY) = FALSE");
mBubbleView = new BubbleView(BubbleActivity.this,tabPosX,tabPosY);
mFrame.addView(mBubbleView);
}
return true;
//return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO - Delegate the touch to the gestureDetector
Log.i(TAG, "Inside onTouchEvent of the main View");
return mGestureDetector.onTouchEvent(event);
}
// BubbleView is a View that displays a bubble.
// This class handles animating, drawing, and popping amongst other actions.
// A new BubbleView is created for each bubble on the display
public class BubbleView extends View {
private static final int BITMAP_SIZE = 64;
private static final int REFRESH_RATE = 40;
private final Paint mPainter = new Paint();
private ScheduledFuture<?> mMoverFuture;
private int mScaledBitmapWidth;
private Bitmap mScaledBitmap;
// location, speed and direction of the bubble
private float mXPos, mYPos, mDx, mDy, mRadius, mRadiusSquared;
private long mRotate, mDRotate;
BubbleView(Context context, float x, float y) {
super(context);
Log.i(TAG, "Inside BubbleView constructor");
// Create a new random number generator to
// randomize size, rotation, speed and direction
Random r = new Random();
// Creates the bubble bitmap for this BubbleView
createScaledBitmap(r);
// Radius of the Bitmap
mRadius = mScaledBitmapWidth / 2;
mRadiusSquared = mRadius * mRadius;
// Adjust position to center the bubble under user's finger
mXPos = x - mRadius;
mYPos = y - mRadius;
}
private void createScaledBitmap(Random r) {
if (speedMode != RANDOM) {
mScaledBitmapWidth = BITMAP_SIZE * 3;
} else {
//TODO - set scaled bitmap size in range [1..3] * BITMAP_SIZE
int min = 1;
int max = 3;
mScaledBitmapWidth = (r.nextInt(max - min + 1) + min)*BITMAP_SIZE;
}
// TODO - create the scaled bitmap using size set above
//mFrame.addView(mBubbleView);
mScaledBitmap = Bitmap.createScaledBitmap(mBitmap, mScaledBitmapWidth, mScaledBitmapWidth, false);
}
// Returns true if the BubbleView intersects position (x,y)
private synchronized boolean intersects(float x, float y) {
// TODO - Return true if the BubbleView intersects position (x,y)
return false;
}
// Cancel the Bubble's movement
// Remove Bubble from mFrame
// Play pop sound if the BubbleView was popped
}
这是完整的LogCat:
08-08 19:48:55.145: I/Lab-Graphics(2318): Before setting up Gesture Detector
08-08 19:48:55.186: I/Lab-Graphics(2318): After setting up Gesture Detector
08-08 19:48:55.194: D/OpenGLRenderer(2318): Render dirty regions requested: true
08-08 19:48:55.194: D/(2318): HostConnection::get() New Host Connection established 0xae0d4d60, tid 2318
08-08 19:48:55.222: D/Atlas(2318): Validating map...
08-08 19:48:55.283: D/(2318): HostConnection::get() New Host Connection established 0xa6b10110, tid 2333
08-08 19:48:55.426: I/OpenGLRenderer(2318): Initialized EGL, version 1.4
08-08 19:48:55.591: D/OpenGLRenderer(2318): Enabling debug mode 0
08-08 19:48:55.661: W/EGL_emulation(2318): eglSurfaceAttrib not implemented
08-08 19:48:55.661: W/OpenGLRenderer(2318): Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0e5da0, error=EGL_SUCCESS
08-08 19:49:00.246: I/Lab-Graphics(2318): Inside onTouchEvent of the main View
08-08 19:49:00.439: I/Lab-Graphics(2318): Inside onTouchEvent of the main View
08-08 19:49:00.558: I/Lab-Graphics(2318): Inside onSingleTapConfirmed of the Gesture Detector
08-08 19:49:00.558: I/Lab-Graphics(2318): mNoOfChildViews: 0
08-08 19:49:00.559: I/Lab-Graphics(2318): pointerIndex: 0 mFrame.getChildCount(): 0 mFrame.getChildAt(pointerIndex):
null
08-08 19:49:00.559: I/Lab-Graphics(2318): mBubbleView is initiated:null
08-08 19:49:00.559: D/AndroidRuntime(2318): Shutting down VM
08-08 19:49:00.560: E/AndroidRuntime(2318): FATAL EXCEPTION: main
08-08 19:49:00.560: E/AndroidRuntime(2318): Process: course.labs.graphicslab, PID: 2318
08-08 19:49:00.560: E/AndroidRuntime(2318): java.lang.NullPointerException: Attempt to invoke direct method 'boolean
course.labs.graphicslab.BubbleActivity$BubbleView.intersects(float, float)' on a null object reference
08-08 19:49:00.560: E/AndroidRuntime(2318): at
course.labs.graphicslab.BubbleActivity$BubbleView.access$0(BubbleActivity.java:368)
08-08 19:49:00.560: E/AndroidRuntime(2318): at
course.labs.graphicslab.BubbleActivity$1.onSingleTapConfirmed(BubbleActivity.java:185)
08-08 19:49:00.560: E/AndroidRuntime(2318): at
android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:273)
08-08 19:49:00.560: E/AndroidRuntime(2318): at android.os.Handler.dispatchMessage(Handler.java:102)
08-08 19:49:00.560: E/AndroidRuntime(2318): at android.os.Looper.loop(Looper.java:135)
08-08 19:49:00.560: E/AndroidRuntime(2318): at android.app.ActivityThread.main(ActivityThread.java:5221)
08-08 19:49:00.560: E/AndroidRuntime(2318): at java.lang.reflect.Method.invoke(Native Method)
08-08 19:49:00.560: E/AndroidRuntime(2318): at java.lang.reflect.Method.invoke(Method.java:372)
08-08 19:49:00.560: E/AndroidRuntime(2318): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-08 19:49:00.560: E/AndroidRuntime(2318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-08 19:49:03.120: I/Process(2318): Sending signal. PID: 2318 SIG: 9
这是main.xml。用户点击屏幕时会以编程方式创建mBubbleView(在这种情况下为mFrame)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF444444"
android:id="@+id/frame"
>
</RelativeLayout>
答案 0 :(得分:0)
您需要在xml中添加气泡视图,例如
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF444444"
android:id="@+id/frame">
<com.your.package.BubbleView id="@+id/bubbleView"></com.your.package.BubbleView>
</RelativeLayout>
并在您的onCreate中,您可以通过ID
获取该气泡视图bubbleView = (BubbleView)findViewById(R.id.bubbleView); //your xml bubble view id