简体   繁体   English

Android上另一个类的调用方法

[英]Call method from another class on Android

I have created two classes, actually both of them extends Activity. 我创建了两个类,实际上它们两个都扩展了Activity。 What I am trying to do is to call a method from the second class. 我想做的是从第二个类调用一个方法。

What I am trying to do is calling the method from second class then implemented in first class, unfortunately I did not have success in that. 我想做的是从第二类中调用该方法,然后在第一类中实现,但是不幸的是我没有成功。

I need your help to solve this problem. 我需要您的帮助来解决这个问题。 Thank you 谢谢

My first class: 我的第一堂课:

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

}

}

The second class: 第二类:

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;

    }

}


}

Make a simple normal java file then define these methods in that class. 制作一个简单的普通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;

        }
    }
}   

Call methods of regular java file in activity like this. 像这样在活动中调用常规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);
    }
}

Why should you do it? 你为什么要这么做?
Why Sounds class extending Activity? 为什么Sounds课堂扩展了Activity?
Please read once again the official documentation Activity . 请再次阅读官方文档Activity

If you did it cause you need a context just pass it like a parameter to the Sounds class. 如果这样做导致需要上下文,则只需将其像参数一样传递给Sounds类。

And you also need to visit Android development guide too 您还需要访问Android开发指南

Only one Activity is instantiated at a time. 一次仅实例化一个Activity。 You should not try to call one Activity from another. 您不应尝试从另一个活动中调用一个活动。

Instead, you should create a third class which contains the method you want to call. 相反,您应该创建第三个类,其中包含要调用的方法。

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;

        }
    }
}

However, you are going to have to do more work with the MediaPlayer. 但是,您将不得不使用MediaPlayer做更多的工作。 You should read the documentation for it before proceeding. 在继续之前,您应该阅读文档。 The code I've shown gives you the basics of what you need to do but it will not work. 我展示的代码为您提供了所需做的基础知识,但无法正常工作。

Finally, best advice I can give you is to learn the basics of Java and OOP before you proceed. 最后,我可以给您的最佳建议是在继续之前学习Java和OOP的基础知识。

Unless testing002 class is actually an Activity you want to use as an Activity, you should move the randomsound... function to a seperate class. 除非test002类实际上是要用作Activity的Activity,否则应将randomsound ...函数移至单独的类。

Like the sounds class but not an Activity. 像声音类,而不是活动。 If you define the function in that class you can construct in another and call it. 如果您在该类中定义函数,则可以在另一个类中进行构造并调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM