在安卓系统中创建游戏重力(续)?

3

编辑:我查看了LogCat,并发现它无法填充com.example.playground。然后我意识到它需要变成com.game.myapp.Playground。在我更改后,它可以正常工作。

我最近询问为什么我的Android应用程序中重力不起作用(链接),但我仍然遇到麻烦。我将视图更改为类“Playground”,但现在它只是强制关闭。我做错了什么?

package com.game.myapp;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;


public class InGame extends Activity{

Playground v;

private int radius;
    private int xPosition;
    private int yPosition;
    private Paint paint;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Rewrite this, it sucks. Seriously.
        super.onCreate(savedInstanceState);
        v = new Playground(this);
        setContentView(v);
    }

    public InGame(int x, int y, int radius, int color)
    { 
        this.xPosition = x; this.yPosition = y; this.radius = radius;
        paint = new Paint(color);
    }

    void moveBall(int x, int y){
         xPosition = x; yPosition =y;        
    } 

    void onDraw(Canvas canvas){
          canvas.drawCircle(xPosition, yPosition, radius, paint);
    }    
}

Playground类:

package com.game.myapp;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

public class Playground extends View{

public static InGame ball;

public Playground(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
} 


   @Override
   public void onDraw(Canvas canvas)
   {
       super.onDraw(canvas);
       if (ball != null ){
           ball.onDraw(canvas);
       }
   }

}

Heres the LogCat:

11-04 16:36:33.945: D/dalvikvm(13177): newInstance failed: no <init>()

11-04 16:36:33.949: D/AndroidRuntime(13177): Shutting down VM

11-04 16:36:33.949: W/dalvikvm(13177): threadid=1: thread exiting with uncaught exception (group=0x4001e578)

11-04 16:36:33.953: E/AndroidRuntime(13177): FATAL EXCEPTION: main

11-04 16:36:33.953: E/AndroidRuntime(13177): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.game.myapp/com.game.myapp.InGame}: java.lang.InstantiationException: com.game.myapp.InGame

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.os.Handler.dispatchMessage(Handler.java:99)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.os.Looper.loop(Looper.java:130)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.ActivityThread.main(ActivityThread.java:3687)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at java.lang.reflect.Method.invokeNative(Native Method)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at java.lang.reflect.Method.invoke(Method.java:507)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at dalvik.system.NativeStart.main(Native Method)

11-04 16:36:33.953: E/AndroidRuntime(13177): Caused by: java.lang.InstantiationException: com.game.myapp.InGame

11-04 16:36:33.953: E/AndroidRuntime(13177):    at java.lang.Class.newInstanceImpl(Native Method)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at java.lang.Class.newInstance(Class.java:1409)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)

11-04 16:36:33.953: E/AndroidRuntime(13177):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)

11-04 16:36:33.953: E/AndroidRuntime(13177):    ... 11 more

2
嗨Jordan。你为什么试图将所有代码放在你的activity中的Ball类中?你不应该尝试将activity作为球使用!你知道,让我为你编写并发布这个会更快!请,请,请听取我的建议。先学习类和面向对象编程,然后再尝试解决Android问题。如果你不理解OOP的基础知识,你将无法成功,并且会浪费很多时间。 - Simon
2个回答

4
这是完整的代码。我刚写并测试了它,它能够正常运行。我在活动中添加了一个计时器,每秒将球向右下移动10个像素。请仔细研究、学习并根据您的需求进行调整。
球类。
package com.example;

import android.graphics.Canvas;
import android.graphics.Paint;

public class Ball{

    private int radius;
    private int xPosition;
    private int yPosition;
    private int color;
    private Paint paint;

    public Ball(int x, int y, int radius, int color)
    {
        this.xPosition = x; this.yPosition = y; this.radius = radius;
        paint = new Paint();
        paint.setColor(color);
    }

    int getX(){return this.xPosition;}
    int getY(){return this.yPosition;}

    void moveBall(int x, int y){
        xPosition = x; yPosition =y;
    }

    void onDraw(Canvas canvas){
        canvas.drawCircle(xPosition, yPosition, radius, paint);
    }

}

游乐场类
package com.example;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;

public class Playground extends ImageView {

    private Ball ball;

    public Playground(Context context) {
        this(context,null);
    }

    public Playground(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Playground(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setBall(Ball ball){
        this.ball = ball;
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if (ball != null ){
            ball.onDraw(canvas);
        }
    }

}

活动类
package com.example;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import java.util.Timer;
import java.util.TimerTask;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */

    Playground playground;
    Ball ball;
    Timer myTimer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        playground = (Playground) findViewById(R.id.playground);

        ball = new Ball(100, 100, 20, Color.RED);

        playground.setBall(ball);

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                Update();
            }

        }, 0, 1000);
    }

    private void Update() {
        this.runOnUiThread(moveBall);
    }

    private Runnable moveBall = new Runnable() {
        public void run() {
            ball.moveBall(ball.getX() + 10, ball.getY() + 10);
            playground.invalidate();
        }
    };


}

[编辑] XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <com.example.Playground
            android:id="@+id/playground"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
</LinearLayout>

OOP的关键点。

小球(Ball)对操场(Playground)或活动(activity)一无所知。它有一个方法,可能会被请求在画布上绘制,但是对于小球而言,它可能是一个不可见的画布、按钮的画布或者是图像位图的画布——它不知道或者说不需要知道。调用该方法的任务是由其他对象来完成的。

操场并不了解活动或小球。它知道自己可能拥有一个小球类的实例,如果确实有,就应该调用它的onDraw方法,但是操场并不知道这个球是什么或者绘制了什么。这是小球需要关注的问题。

活动除了拥有一个小球和操场之外,对它们一无所知,只需调用小球的移动方法然后告诉操场重绘自己即可。

这意味着,你可以更改绘制方法、移动方法和其他所有内容而无需重新编码(在一般情况下)。例如,你可以将小球类中的onDraw更改为绘制矩形。当然,现在类的名称已经是一个糟糕的选择,但你明白这个意思...


哇!现在它有了更多的意义!我使用了完全相同的代码,但是它仍然强制关闭。作为Activity类,我将我的主类MainActivity和MainActivity的xml文件activity_main.xml放在了一起。我不确定发生了什么事情,所以我现在正在上传LogCat。谢谢你,看起来更加清晰简单了! - Jordan
另外,一旦我解决了这个问题,我会先尝试解决面向对象编程,因为我(以及你)感觉如果我先学习它,Android会更容易些。 - Jordan
我在playground类中遇到了一个错误:在public setBall(Ball ball)中,它说“缺少方法的返回类型”,所以它想让我将其更改为public void。但是当我更改后,它就强制关闭了。LogCat在帖子中。 - Jordan
我需要将com.example.Playground更改为我的包名Playground。感谢您的所有帮助!现在我要学习面向对象编程 :) - Jordan

2

你的InGameActivity中没有默认构造函数,这对于Android实例化它是必要的。

显式构造函数的存在将导致未定义隐式无参构造函数。您可能需要提供自己的无参构造函数来将其成员初始化为默认值。

我会删除显式构造函数并将初始化放入onCreate(Bundle)方法中。


谢谢。我该如何添加一个无参构造函数? - Jordan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接