我创建了两个类,实际上它们都扩展了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;
}
}
}
答案 0 :(得分:8)
创建一个简单的普通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);
}
}
答案 1 :(得分:2)
你为什么要这样做?
为什么Sounds
类扩展活动?
请再次阅读官方文档Activity。
如果你这样做,因为你需要一个上下文,只需将它作为参数传递给Sounds
类。
答案 2 :(得分: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的基础知识。
答案 3 :(得分:0)
除非testing002类实际上是您想要用作Activity的Activity,否则您应该将randomsound ...函数移动到单独的类。
与声音类类似但不是活动。如果在该类中定义函数,则可以在另一个函数中构造并调用它。