java 从 Android 上的另一个类调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14671307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call method from another class on Android
提问by Mohammed Asmar
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.
我创建了两个类,实际上它们都扩展了 Activity。我想要做的是从第二个类调用一个方法。
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;
}
}
}
回答by Ajay S
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);
}
}
回答by EvZ
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.
你为什么要这样做?
为什么要Sounds
类扩展 Activity?
请再次阅读官方文档Activity。
如果你这样做是因为你需要一个上下文,只需将它像参数一样传递给Sounds
类。
And you also need to visit Android development guidetoo
而且你还需要访问Android开发指导过
回答by Simon
Only one Activity is instantiated at a time. You should not try to call one Activity from another.
一次仅实例化一个 Activity。您不应该尝试从另一个 Activity 调用一个 Activity。
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.
但是,您将不得不使用 MediaPlayer 做更多的工作。在继续之前,您应该阅读它的文档。我展示的代码为您提供了需要做的事情的基础知识,但它不起作用。
Finally, best advice I can give you is to learn the basics of Java and OOP before you proceed.
最后,我能给你的最好建议是在继续之前学习 Java 和 OOP 的基础知识。
回答by RvdK
Unless testing002 class is actually an Activity you want to use as an Activity, you should move the randomsound... function to a seperate class.
除非 testing002 类实际上是您想要用作 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.
像声音类,但不是活动。如果您在该类中定义函数,则可以在另一个类中构造并调用它。