Android 如何更改三个片段上的图像

Android 如何更改三个片段上的图像,android,static,fragment,Android,Static,Fragment,我有一个三段式的项目。我想将每个片段上的图像从播放图像更改为暂停图像。我有一个公共课叫Global。我想从FragmentA调用Global.setCntrImage()例程。此例程将更改三个片段上的三个图像 在这里,如果碎片是全局类的调用 if (Global.player.isPlaying()) { Global.stopPlay(); image.setImageResourc

我有一个三段式的项目。我想将每个片段上的图像从播放图像更改为暂停图像。我有一个公共课叫Global。我想从FragmentA调用Global.setCntrImage()例程。此例程将更改三个片段上的三个图像

在这里,如果碎片是全局类的调用

                if (Global.player.isPlaying()) {    
                  Global.stopPlay();
                  image.setImageResource(R.drawable.play);
                  Global.setCntrlImage("play");
                  Global.playing = false;
                     }
public class Global  {

public static boolean playing = false;
public static MediaPlayer player = new MediaPlayer();

public static void stopPlay() {
    //Log.d(APP_TAG, "stopping playback..");
    //printResult("stop playing..");
    player.pause();
    player.reset();
    playing = false;
}

public static void startPlay(Context context, String fn) {
    // TODO Auto-generated method stub

        //Log.d(APP_TAG, "starting playback..");
        final String APP_TAG = "com.hascode.android.soundrecorder";

        if (player.isPlaying()){
            player.stop();
            player.reset();
            player.seekTo(0);
        }

        try {
            AssetFileDescriptor descriptor = context.getResources().getAssets().openFd(fn);
            player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
            player.setLooping(true);
            player.prepare();
            player.start();
            playing = true;
        } catch (IllegalStateException e) {
             //Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
             Log.w(APP_TAG, "illegal state .. player should be reset");
        } catch (IOException e) {
             //Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
             Log.w(APP_TAG, "Could not write to sd card");
        }
        //System.out.println("playing = " + playing);
        //System.out.println("Song duration is " + player.getDuration());       
}


public static void setCntrlImage(String cntrl){
    //get image object from fragmentB, How?
    ImageView image = (ImageView) getView().findViewById(R.id.imageView1);
    if (cntrl == "play"){
        image.setImageResource(R.drawable.play);
    }else {
        image.setImageResource(R.drawable.pause);
    }       
}   
这是我的全球课程

                if (Global.player.isPlaying()) {    
                  Global.stopPlay();
                  image.setImageResource(R.drawable.play);
                  Global.setCntrlImage("play");
                  Global.playing = false;
                     }
public class Global  {

public static boolean playing = false;
public static MediaPlayer player = new MediaPlayer();

public static void stopPlay() {
    //Log.d(APP_TAG, "stopping playback..");
    //printResult("stop playing..");
    player.pause();
    player.reset();
    playing = false;
}

public static void startPlay(Context context, String fn) {
    // TODO Auto-generated method stub

        //Log.d(APP_TAG, "starting playback..");
        final String APP_TAG = "com.hascode.android.soundrecorder";

        if (player.isPlaying()){
            player.stop();
            player.reset();
            player.seekTo(0);
        }

        try {
            AssetFileDescriptor descriptor = context.getResources().getAssets().openFd(fn);
            player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
            player.setLooping(true);
            player.prepare();
            player.start();
            playing = true;
        } catch (IllegalStateException e) {
             //Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
             Log.w(APP_TAG, "illegal state .. player should be reset");
        } catch (IOException e) {
             //Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
             Log.w(APP_TAG, "Could not write to sd card");
        }
        //System.out.println("playing = " + playing);
        //System.out.println("Song duration is " + player.getDuration());       
}


public static void setCntrlImage(String cntrl){
    //get image object from fragmentB, How?
    ImageView image = (ImageView) getView().findViewById(R.id.imageView1);
    if (cntrl == "play"){
        image.setImageResource(R.drawable.play);
    }else {
        image.setImageResource(R.drawable.pause);
    }       
}   
未定义全局类型的方法getView()。如何更改三个片段(A、B和C)上的图像


谢谢,

为什么不通过您的活动来处理这个问题呢?然后您可以在全局类中创建一个iterface,并在您的片段中实现该接口。在那里,你可以得到即时回拨,只要你点击暂停按钮。在该回调方法中,更改片段的imagaview。或者,您可以编写一个Singleton Iamgeview类,将其放入这三个片段中,从全局类中,您可以通过调用单个方法将图像设置为所有片段。请参阅链接:谢谢,我将尝试处理我的活动。