需要由另一个java类访问1个java类的方法

需要由另一个java类访问1个java类的方法,java,android,Java,Android,我试图通过另一个java类从一个java类访问一个方法。我已经创建了类的对象,并将该类导入到我的java类中,但我无法在代码中访问它 package com.example.musicplayer; import java.io.File; import java.util.ArrayList; import java.util.List; import android.R.string; import android.app.ListActivity; import andro

我试图通过另一个java类从一个java类访问一个方法。我已经创建了类的对象,并将该类导入到我的java类中,但我无法在代码中访问它

   package com.example.musicplayer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



import android.R.string;

import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Playlists;
import android.test.suitebuilder.TestSuiteBuilder.FailedToCreateTests;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


        public class MainActivity extends ListActivity {

    public static final String MEDIA_PATH = new String("/sdcard/");
    private List<String> songs = new ArrayList<String>();
    public MediaPlayer mp = new MediaPlayer();
    private int currentPosition = 0;

    public MediaPlayer getmp(){
        return mp;
    }
    public void setmp(MediaPlayer mp){
        this.mp = mp;
    }

       @Override
          protected void onCreate(Bundle icicle) {
             super.onCreate(icicle);
           setContentView(R.layout.activity_main);
         updateSongList();         
     }

            private void updateSongList() {
        // TODO Auto-generated method stub
        File home = new File(MEDIA_PATH);
        if(home.listFiles(new Mp3Filter()).length > 0){
            for(File file: home.listFiles(new Mp3Filter())){
                songs.add(file.getName());
            }
          }
                ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.song_item, songs);


        setListAdapter(songList);
      }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
        currentPosition = position;
        playSong(MEDIA_PATH + songs.get(position));

        //Toast.makeText(getApplicationContext(), songs.get(position).toString(), Toast.LENGTH_SHORT).show();

        final String songName = songs.get(position).toString();
        //final TextView textchange = (TextView) v.findViewById(R.id.current_song_name);

        Intent in = new Intent(this, current_song.class);
        in.putExtra("song_name", songName);
        startActivity(in);

        //Toast.makeText(getApplicationContext(), "End of Song List", Toast.LENGTH_SHORT).show();       
        //textchange.setText(songName);
    }


    private void playSong(String songPath) {
        // TODO Auto-generated method stub

        try{

            mp.reset();
            mp.setDataSource(songPath);
            mp.prepare();
            mp.start();

            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    auto_nextSong();
                }               
            });

        }
        catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void pause(){
        mp.pause();
    }

    public void stop(){

        mp.stop();
    }

    public void next(){ 
        Toast.makeText(getApplicationContext(), "In Next()", Toast.LENGTH_SHORT).show();
        if(currentPosition < songs.size()){
            currentPosition = currentPosition + 1;
            playSong(MEDIA_PATH + songs.get(currentPosition));
        }
    }

    public void prv(){
        if(currentPosition > songs.size()){
            currentPosition = currentPosition - 1;      
            playSong(MEDIA_PATH + songs.get(currentPosition));
        }
    }

    private void auto_nextSong() {
        // TODO Auto-generated method stub
        if(++currentPosition >= songs.size()){
            currentPosition = 0;
            Toast.makeText(getApplicationContext(), "End of Song List", Toast.LENGTH_SHORT).show();
        }
        else{
            playSong(MEDIA_PATH + songs.get(currentPosition));
            Toast.makeText(getApplicationContext(), "Next Song", Toast.LENGTH_SHORT).show();
        }
    }

     }

我想停下来,暂停正在播放的音乐。如果我的逻辑错误,请指导我如何做,否则请帮助我如何控制当前音乐java类中的当前音乐

请将您所有的音乐播放代码移到服务中。活动不是这样的

考虑以下几点:

我认为您是android新手,但没有问题:请看,您不能从任何其他类(Object.method();)调用activity函数

因此,对于这种类型的处理,您应该创建一个
内部处理程序类
,它
在MainActivity中扩展处理程序。
并重写它的onHandle(message msg){}方法

当您启动当前的歌曲活动时,在意图中传递此处理程序对象。

当您想从MainActivity执行任何任务(如停止、暂停)时,
通过该处理程序传递消息

在您的内部onHandle()方法通过从message对象接收的不同命令调用停止或暂停方法。


类似于
if(消息的内容==stop)MainActivity.this.stop()

使用ma.pause()和ma.stop(),无需将ma强制转换为MainActivity,因为这是它的类型。如果我没有错,您是否正在尝试从一个活动访问另一个活动的方法?我不认为你能在安卓系统中做到这一点。此外,即使在Java中,如果不在类B中实例化类a,您也无法从另一个类B访问类a的非静态方法。如果您能告诉我们您在代码中试图做什么,我们可以为您推荐其他方法。是的,我对android完全陌生。你能给我举个例子吗?当然,我正在为此添加另一个答案,给我10分钟时间。@shidutt即使你还没有添加答案。
package com.example.musicplayer;



     import com.example.musicplayer.MainActivity;
             import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

        public class current_song extends Activity implements OnClickListener{


     public MainActivity ma;
     protected void onCreate(Bundle icicle, MainActivity ma) {

         Bundle extra = getIntent().getExtras();

         super.onCreate(icicle);
         setContentView(R.layout.songplay_page);

         if(extra != null){
             String song_name = extra.getString("song_name");
             TextView textchange = (TextView)findViewById(R.id.current_song_name);
             textchange.setText(song_name);
             textchange.setSelected(true);
         }       

         //MainActivity ma = ((MainActivity)getApplicationContext());
         //MediaPlayer mp = ma.getmp();

         this.ma = ma;
         Button btn_pause = (Button)findViewById(R.id.pause_btn);
         btn_pause.setOnClickListener(this);    

         Button btn_next = (Button)findViewById(R.id.next_btn);
         btn_next.setOnClickListener(this);

         Button btn_prv = (Button)findViewById(R.id.prv_btn);
         btn_prv.setOnClickListener(this);

         Button btn_stop = (Button)findViewById(R.id.stop_btn);
         btn_stop.setOnClickListener(this);

     }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(), "In Onclick ()", Toast.LENGTH_SHORT).show();
        switch(v.getId())
        {
            case R.id.pause_btn:
                ((MainActivity)ma).pause();
            break;

            case R.id.next_btn:
                //ma.next();
            break;

            case R.id.prv_btn:
                //ma.prv();
            break;

            case R.id.stop_btn:
                ((MainActivity)ma).stop();
            break;
        }

    }

       }