为什么我的Android游戏会崩溃?

时间:2011-10-28 16:30:50

标签: android

在建立我自己的游戏的道路上,我遇到了一个很大的错误。我查看了所有代码以找到错误的内容,但我找不到它。

我的应用程序的代码是这样的:

显示包含5个按钮和1个标签的菜单。如果单击新游戏按钮,则调用将启动游戏类的startgame()函数;这将启动游戏视图课程。之后在屏幕上应该只显示一个彩色屏幕,仅此而已,但它不会......它实际上崩溃了。

以下是代码:

BeginActivity.Java:

    import android.content.Intent;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.app.AlertDialog;
    import android.util.Log;
    import android.content.DialogInterface;

    public class BeginActivity extends Activity implements OnClickListener {

public void onClick(View v) {
    switch (v.getId()){
    case R.id.newgame:
        startgame();
        break;
    }
    // TODO Auto-generated method stub

 }


 /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     View continueButton = findViewById(R.id.continuebutton);
     continueButton.setOnClickListener(this);

     View newButton = findViewById(R.id.newgame);
     newButton.setOnClickListener(this);

    View aboutButton = findViewById(R.id.aboutbutton);
    aboutButton.setOnClickListener(this);

    View achievButton = findViewById(R.id.achievbutton);
    achievButton.setOnClickListener(this);

    View exitButton = findViewById(R.id.exit);
    exitButton.setOnClickListener(this);




    }
    private void startgame() {

    Intent intent = new Intent(ThinkyoursmartActivity.this, Game.class);
    startActivity(intent);


     }
  }

Game.Java:

    public class Game extends Activity {

private Gameview Gameview;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Gameview = new Gameview(this);
      setContentView(Gameview);
      Gameview.requestFocus();
   }

     }

GameView.Java:

    public class Gameview extends View{

private final Game Game;
public Gameview(Context context) {
    super(context);
    this.Game = (Game) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
    // TODO Auto-generated constructor stub
}

@Override
   protected void onDraw(Canvas canvas) {
    // Draw the background...
      Paint background = new Paint();
      background.setColor(getResources().getColor(
            R.color.puzzle_background));

      canvas.drawRect(0, 0, getWidth(), getHeight(), background);


}
    }

在日志上:

10-28 16:38:39.949: ERROR/AndroidRuntime(333): FATAL EXCEPTION: main
10-28 16:38:39.949: ERROR/AndroidRuntime(333): android.content.ActivityNotFoundException: Unable to find explicit activity class {think.smart/think.smart.Game}; have you declared this activity in your AndroidManifest.xml?
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.app.Activity.startActivityForResult(Activity.java:2827)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.app.Activity.startActivity(Activity.java:2933)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at think.smart.ThinkyoursmartActivity.startgame(ThinkyoursmartActivity.java:58)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at think.smart.ThinkyoursmartActivity.onClick(ThinkyoursmartActivity.java:22)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.view.View.performClick(View.java:2485)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.view.View$PerformClick.run(View.java:9080)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.os.Handler.handleCallback(Handler.java:587)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.os.Looper.loop(Looper.java:123)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at java.lang.reflect.Method.invoke(Method.java:507)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-28 16:38:39.949: ERROR/AndroidRuntime(333):     at dalvik.system.NativeStart.main(Native Method)
10-28 16:38:39.959: WARN/ActivityManager(67):   Force finishing activity think.smart/.ThinkyoursmartActivity
10-28 16:38:40.480: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{406ee178 think.smart/.ThinkyoursmartActivity}
10-28 16:38:51.975: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{406ee178 think.smart/.ThinkyoursmartActivity}

1 个答案:

答案 0 :(得分:0)

请添加您的Exception的跟踪跟踪,并且作为JAVA中的约定,您的变量应该以小写字母开头(不是大写:私有GameView gameView;更可接受:))。

因此,请检查您的Manifest文件中是否已声明所有活动:

<application>
<activity android:name="YourActivity" />

</application>