2013-02-03 57 views
0

我創建了兩個類,實際上它們都擴展了Activity。我想要做的是從第二類調用一個方法。來自Android上另一個類的調用方法

我想要做的是從第二類調用方法,然後在第一類中實現,不幸的是我沒有成功。

我需要你的幫助來解決這個問題。謝謝

我的第一類:

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

爲什麼你創造了大量只使用整數數組或ArrayList中存儲的文件編號,並根據隨機數 –

+0

我知道如何做到這一點 –

+0

分配給MediaPlayer的實例MediaPlayer的情況,但這是不是如果然後ü知道它的問題 –

回答

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); 
    } 
} 
+0

它是五我錯誤創建 –

+0

酷= MediaPlayer.create(this,R.raw.cool); –

+0

@MohammedAsmar什麼錯誤? –

2

你爲什麼要這麼做?
爲什麼Sounds類擴展Activity?
請再次閱讀官方文檔Activity

如果你這樣做,你需要一個上下文,只需將它作爲參數傳遞給Sounds類。

而且你還需要訪問Android development guide

+0

+1讓他學習:) – AAnkit

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的基礎知識。

0

除非testing002類實際上是一個您想用作Activity的Activity,您應該將randomsound ...函數移動到一個單獨的類。

像聲音類,但不是活動。如果您在該類中定義該函數,則可以在另一個類中構造並調用它。