简体   繁体   中英

Call method from another class on Android

I have created two classes, actually both of them extends 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.

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.

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?
Please read once again the official documentation Activity .

If you did it cause you need a context just pass it like a parameter to the Sounds class.

And you also need to visit Android development guide too

Only one Activity is instantiated at a time. 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. 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.

Unless testing002 class is actually an Activity you want to use as an Activity, you should move the randomsound... function to a seperate class.

Like the sounds class but not an Activity. If you define the function in that class you can construct in another and call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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