【问题标题】:Creating game gravity in Android (continued)?在 Android 中创建游戏重力(续)?
【发布时间】:2012-11-05 20:50:46
【问题描述】:

编辑:我查看了 LogCat,它说它不能为 com.example.playground 充气。然后我意识到它需要我 com.game.myapp.Playground。我改了之后就可以了。

我最近问为什么重力在我的 Android 应用 (link) 中不起作用,但我仍然遇到问题。我将视图更改为“游乐场”类,但现在它只是强制关闭。我究竟做错了什么?

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);
    }    
}

游乐场类:

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);
       }
   }

}

这是 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

【问题讨论】:

  • 嗨,乔丹。为什么您尝试将所有代码放在您的活动中的 Ball 类中?您不应该尝试将活动用作球!你知道,我为你写这篇文章并发布它会更快!请,请,请接受我的建议。了解类和面向对象的编程,然后尝试处理 Android。如果您不了解 OOP 的基础知识,您将不会成功,并且会在此过程中浪费大量时间。

标签: android eclipse logcat gravity


【解决方案1】:

这是完整的代码。我刚刚写了这个并测试了它。有用。我在活动中添加了一个计时器,以每秒将球向右和向下移动 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 或活动。它有一个方法,可能会被某个要求它在画布上绘制的东西调用,但据它所知,它可能是一个不可见的画布,或者按钮的画布,或者位图的画布- 它不知道或不需要知道。这是调用该方法的任何人的工作。

操场对活动或球一无所知。它知道它可能有一个 Ball 类的实例,如果有,它应该调用它的 onDraw 方法,但它不知道球是什么或它画了什么。小球对此很担心。

Activity 不知道 Ball 或 Playground,只知道其中之一,然后调用 Ball move 方法,然后告诉 Playground 重绘自己。

关键是,您可以更改绘图方法、移动方法和其他所有内容,而无需重新编码(一般来说,好的)。例如,您可以更改 Ball 类中的 onDraw 以绘制一个矩形。当然,类的名称现在是一个糟糕的选择,但你明白了......

【讨论】:

  • 哇!现在更有意义了!我使用了确切的代码,但它强制关闭。作为 Activity 类,我放置了我的主类 MainActivity 和 MainActivity 中的 xml,activity_main.xml。我不确定发生了什么事,所以我现在正在安装 LogCat。不过谢谢,它看起来更干净、更容易!
  • 另外,一旦我解决了这个问题,我会先尝试解决 OOP,我(和你)觉得如果我先学会这一点,Android 会容易得多。
  • 我在操场类中遇到错误:在 public setBall(Ball ball) 中,它显示“方法的返回类型丢失”,所以它希望我将其更改为 public void。但是当我改变它时,它会强制关闭。 LogCat 在帖子中。
  • 我需要将 com.example.Playground 更改为我的包名.Playground。感谢您所有的帮助!我现在要学习 OOP :)
【解决方案2】:

InGameActivity 中没有默认构造函数,这是 Android 能够实例化它所必需的。

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

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

【讨论】:

  • 谢谢。如何添加无参数构造函数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多