0

我创建了两个类,实际上它们都扩展了 Activity。我想做的是从第二类调用一个方法。

我正在尝试做的是从第二类调用该方法,然后在第一类中实现,不幸的是我没有成功。

我需要你的帮助来解决这个问题。谢谢

我的第一堂课:

package com.math4kids;

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

public class testing002 extends Activity {

private Sounds myotherclass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.numeracy);

    myotherclass.Randomsoundforrightanswer();

}

}

第二类:

package com.math4kids;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;

public class Sounds extends Activity {

MediaPlayer cool, good, perfect, sweet, excellent, goodthinking, greatjob,
        notbad, thatstheway, youdidit, yes, again, wrong, sorry,
        sundfornum01, sundfornum02;
public Random random = new Random();

public Sounds(Context context){
    super.getApplicationContext();
}

public void Randomsoundforrightanswer() {
    cool = MediaPlayer.create(this, R.raw.cool);
    good = MediaPlayer.create(this, R.raw.good);
    perfect = MediaPlayer.create(this, R.raw.perfect);
    sweet = MediaPlayer.create(this, R.raw.sweet);
    excellent = MediaPlayer.create(this, R.raw.excellent);
    goodthinking = MediaPlayer.create(this, R.raw.goodthinking);
    greatjob = MediaPlayer.create(this, R.raw.greatjob);
    notbad = MediaPlayer.create(this, R.raw.notbad);
    thatstheway = MediaPlayer.create(this, R.raw.thatstheway);
    youdidit = MediaPlayer.create(this, R.raw.youdidit);
    yes = MediaPlayer.create(this, R.raw.yes);

    switch (random.nextInt(11)) {

    case 0:
        cool.start();
        break;
    case 1:
        good.start();
        break;
    case 2:
        perfect.start();
        break;
    case 3:
        sweet.start();
        break;
    case 4:
        excellent.start();
        break;
    case 5:
        goodthinking.start();
        break;
    case 6:
        greatjob.start();
        break;
    case 7:
        notbad.start();
        break;
    case 8:
        thatstheway.start();
        break;
    case 9:
        youdidit.start();
        break;
    case 10:
        yes.start();
        break;

    }

}


}
4

4 回答 4

9

制作一个简单的普通 java 文件,然后在该类中定义这些方法。

import java.util.Random;
import android.media.MediaPlayer;

public class Sounds {

    Context context;
    MediaPlayer cool, good, perfect, sweet, excellent, goodthinking, greatjob,
        notbad, thatstheway, youdidit, yes, again, wrong, sorry,
        sundfornum01, sundfornum02;
    public Random random = new Random();

    public Sounds(Context context){
        this.context = context;
    }

    public void Randomsoundforrightanswer() {
        cool = MediaPlayer.create(context, R.raw.cool);
        good = MediaPlayer.create(context, R.raw.good);
        perfect = MediaPlayer.create(context, R.raw.perfect);
        sweet = MediaPlayer.create(context, R.raw.sweet);
        excellent = MediaPlayer.create(context, R.raw.excellent);
        goodthinking = MediaPlayer.create(context, R.raw.goodthinking);
        greatjob = MediaPlayer.create(context, R.raw.greatjob);
        notbad = MediaPlayer.create(context, R.raw.notbad);
        thatstheway = MediaPlayer.create(context, R.raw.thatstheway);
        youdidit = MediaPlayer.create(context, R.raw.youdidit);
        yes = MediaPlayer.create(context, R.raw.yes);

        switch (random.nextInt(11)) {

            case 0:
                cool.start();
                break;
            case 1:
                good.start();
                break;
            case 2:
                perfect.start();
                break;
            case 3:
                sweet.start();
                break;
            case 4:
                excellent.start();
                break;
            case 5:
                goodthinking.start();
                break;
            case 6:
                greatjob.start();
                break;
            case 7:
                notbad.start();
                break;
            case 8:
                thatstheway.start();
                break;
            case 9:
                youdidit.start();
                break;
            case 10:
                yes.start();
                break;

        }
    }
}   

像这样在活动中调用常规java文件的方法。

import android.app.Activity;
import android.os.Bundle;
public class testing002 extends Activity {
private Sounds myotherclass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.numeracy);
        new Sounds().Randomsoundforrightanswer(this);
    }
}
于 2013-02-03T10:03:31.340 回答
2

你为什么要这样做?
为什么Sounds类扩展 Activity?
请再次阅读官方文档Activity

如果你这样做是因为你需要一个上下文,只需将它作为参数传递给Sounds类。

您还需要访问Android 开发指南

于 2013-02-03T10:02:37.480 回答
0

一次只实例化一个 Activity。您不应该尝试从另一个活动中调用一个活动。

相反,您应该创建第三个类,其中包含您要调用的方法。

public class SoundManager{

    private context;

    public SoundManager(Context context){

        context.context = context;

    }

    public void Randomsoundforrightanswer() {

        cool = MediaPlayer.create(context, R.raw.cool);
        good = MediaPlayer.create(context, R.raw.good);
        perfect = MediaPlayer.create(context, R.raw.perfect);
        sweet = MediaPlayer.create(context, R.raw.sweet);
        excellent = MediaPlayer.create(context, R.raw.excellent);
        goodthinking = MediaPlayer.create(context, R.raw.goodthinking);
        greatjob = MediaPlayer.create(context, R.raw.greatjob);
        notbad = MediaPlayer.create(context, R.raw.notbad);
        thatstheway = MediaPlayer.create(context, R.raw.thatstheway);
        youdidit = MediaPlayer.create(context, R.raw.youdidit);
        yes = MediaPlayer.create(context, R.raw.yes);

        switch (random.nextInt(11)) {

        case 0:
            cool.start();
            break;
        case 1:
            good.start();
            break;
        case 2:
            perfect.start();
            break;
        case 3:
            sweet.start();
            break;
        case 4:
            excellent.start();
            break;
        case 5:
            goodthinking.start();
            break;
        case 6:
            greatjob.start();
            break;
        case 7:
            notbad.start();
            break;
        case 8:
            thatstheway.start();
            break;
        case 9:
            youdidit.start();
            break;
        case 10:
            yes.start();
            break;

        }
    }
}

但是,您将不得不对 MediaPlayer 做更多的工作。在继续之前,您应该阅读它的文档。我展示的代码为您提供了您需要做的基础知识,但它不起作用。

最后,我能给你的最好建议是在继续之前学习 Java 和 OOP 的基础知识。

于 2013-02-03T10:04:37.367 回答
0

除非 testing002 类实际上是您想用作 Activity 的 Activity,否则您应该将 randomsound... 函数移到单独的类中。

像声音类但不是活动。如果您在该类中定义函数,则可以在另一个类中构造并调用它。

于 2013-02-03T10:05:49.100 回答