我开始构建一个应用程序。我一直试图开始它,我得到了一个崩溃说"不幸的是," App Name"已经停止"
我不知道如何解决这个问题。
LogCat中的错误消息:
02-16 03:51:29.450: D/AndroidRuntime(907): Shutting down VM
02-16 03:51:29.450: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0xb1a3bba8)
02-16 03:51:29.470: E/AndroidRuntime(907): FATAL EXCEPTION: main
02-16 03:51:29.470: E/AndroidRuntime(907): Process: com.waldhirsch.diggers, PID: 907
02-16 03:51:29.470: E/AndroidRuntime(907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.waldhirsch.diggers/com.waldhirsch.diggers.FullscreenActivity}: java.lang.NullPointerException
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:102)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:136)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-16 03:51:29.470: E/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 03:51:29.470: E/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:515)
02-16 03:51:29.470: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-16 03:51:29.470: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-16 03:51:29.470: E/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method)
02-16 03:51:29.470: E/AndroidRuntime(907): Caused by: java.lang.NullPointerException
02-16 03:51:29.470: E/AndroidRuntime(907): at com.waldhirsch.diggers.util.SystemUiHiderHoneycomb.setup(SystemUiHiderHoneycomb.java:74)
02-16 03:51:29.470: E/AndroidRuntime(907): at com.waldhirsch.diggers.FullscreenActivity.onCreate(FullscreenActivity.java:60)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.Activity.performCreate(Activity.java:5231)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-16 03:51:29.470: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-16 03:51:29.470: E/AndroidRuntime(907): ... 11 more
FullscreenActivity.java
package com.waldhirsch.diggers;
import com.waldhirsch.diggers.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* @see SystemUiHider
*/
public class FullscreenActivity extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {@link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {@link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
GameLoopThread.java
package com.waldhirsch.diggers;
import android.graphics.Canvas;
public class GameLoopThread extends Thread {
private GameView theView;
private boolean isRunning = false;
public GameLoopThread(GameView theView) {
this.theView = theView;
}
public void setRunning(boolean run) {
isRunning = run;
}
@Override
public void run() {
while (isRunning) {
Canvas theCanvas = null;
try {
theCanvas = theView.getHolder().lockCanvas();
synchronized (theView.getHolder()) {
theView.draw(theCanvas);
}
} finally {
if (theCanvas != null) {
theView.getHolder().unlockCanvasAndPost(theCanvas);
}
}
}
}
}
GameView.java
package com.waldhirsch.diggers;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
private SurfaceHolder surfaceHolder;
private Bitmap bmp;
private int y=0;
private GameLoopThread theGameLoopThread;
public GameView(Context context) {
super(context);
theGameLoopThread = new GameLoopThread(this);
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
theGameLoopThread.setRunning(false);
while(retry){
try {
theGameLoopThread.join();
retry=false;
}catch(InterruptedException e){
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
theGameLoopThread.setRunning(true);
theGameLoopThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
}
@Override
public void draw(Canvas canvas) {
canvas.drawColor(Color.DKGRAY);
if(y <= getHeight() - bmp.getHeight()){
y=y+3;
}
canvas.drawBitmap(bmp, 25, y, null);
}
}
SystemUiHiderHoneycomb.java
package com.waldhirsch.diggers.util;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
/**
* An API 11+ implementation of {@link SystemUiHider}. Uses APIs available in
* Honeycomb and later (specifically {@link View#setSystemUiVisibility(int)}) to
* show and hide the system UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SystemUiHiderHoneycomb extends SystemUiHiderBase {
/**
* Flags for {@link View#setSystemUiVisibility(int)} to use when showing the
* system UI.
*/
private int mShowFlags;
/**
* Flags for {@link View#setSystemUiVisibility(int)} to use when hiding the
* system UI.
*/
private int mHideFlags;
/**
* Flags to test against the first parameter in
* {@link android.view.View.OnSystemUiVisibilityChangeListener#onSystemUiVisibilityChange(int)}
* to determine the system UI visibility state.
*/
private int mTestFlags;
/**
* Whether or not the system UI is currently visible. This is cached from
* {@link android.view.View.OnSystemUiVisibilityChangeListener}.
*/
private boolean mVisible = true;
/**
* Constructor not intended to be called by clients. Use
* {@link SystemUiHider#getInstance} to obtain an instance.
*/
protected SystemUiHiderHoneycomb(Activity activity, View anchorView, int flags) {
super(activity, anchorView, flags);
mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE;
mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if ((mFlags & FLAG_FULLSCREEN) != 0) {
// If the client requested fullscreen, add flags relevant to hiding
// the status bar. Note that some of these constants are new as of
// API 16 (Jelly Bean). It is safe to use them, as they are inlined
// at compile-time and do nothing on pre-Jelly Bean devices.
mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
}
if ((mFlags & FLAG_HIDE_NAVIGATION) != 0) {
// If the client requested hiding navigation, add relevant flags.
mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
mTestFlags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
}
/** {@inheritDoc} */
@Override
public void setup() {
mAnchorView.setOnSystemUiVisibilityChangeListener(mSystemUiVisibilityChangeListener);
}
/** {@inheritDoc} */
@Override
public void hide() {
mAnchorView.setSystemUiVisibility(mHideFlags);
}
/** {@inheritDoc} */
@Override
public void show() {
mAnchorView.setSystemUiVisibility(mShowFlags);
}
/** {@inheritDoc} */
@Override
public boolean isVisible() {
return mVisible;
}
private View.OnSystemUiVisibilityChangeListener mSystemUiVisibilityChangeListener
= new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int vis) {
// Test against mTestFlags to see if the system UI is visible.
if ((vis & mTestFlags) != 0) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// Pre-Jelly Bean, we must manually hide the action bar
// and use the old window flags API.
mActivity.getActionBar().hide();
mActivity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// Trigger the registered listener and cache the visibility
// state.
mOnVisibilityChangeListener.onVisibilityChange(false);
mVisible = false;
} else {
mAnchorView.setSystemUiVisibility(mShowFlags);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// Pre-Jelly Bean, we must manually show the action bar
// and use the old window flags API.
mActivity.getActionBar().show();
mActivity.getWindow().setFlags(
0,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// Trigger the registered listener and cache the visibility
// state.
mOnVisibilityChangeListener.onVisibilityChange(true);
mVisible = true;
}
}
};
}