Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据可见的片段更改单击时发生的情况?_Java_Android_Android Fragments_Onclicklistener - Fatal编程技术网

Java 根据可见的片段更改单击时发生的情况?

Java 根据可见的片段更改单击时发生的情况?,java,android,android-fragments,onclicklistener,Java,Android,Android Fragments,Onclicklistener,我有一个带有图像的布局,根据图像下方框架中显示的片段,单击该布局时应该会播放声音 我所有的片段都在内部类中 当我开始活动时,我得到一个力接近。问题是 这段代码包含在我的三个片段中,但声音不同,sound1代表片段,sound2代表片段2,等等 public class Fragment_gehorINTbeginnerR8 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater,

我有一个带有图像的布局,根据图像下方框架中显示的片段,单击该布局时应该会播放声音

我所有的片段都在内部类中

当我开始活动时,我得到一个力接近。问题是

这段代码包含在我的三个片段中,但声音不同,sound1代表片段,sound2代表片段2,等等

public class Fragment_gehorINTbeginnerR8 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  {

        if (container == null) {
            return null;
        }

        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_musikteori_gehortest_int_beg, container, false);

        Button buttonR1 = (Button) mLinearLayout.findViewById(R.id.button_r1);
        Button buttonR5 = (Button) mLinearLayout.findViewById(R.id.button_r5);
        Button buttonR8 = (Button) mLinearLayout.findViewById(R.id.button_r8);


        ImageView imagePlay = (ImageView) findViewById(R.id.imageViewPlayGehorSound);

        if(mp != null && mp.isPlaying()){
            mp.stop();

        }
        mp = MediaPlayer.create(getApplicationContext(), R.raw.inter_oktav);
        mp.start();

        imagePlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp != null && mp.isPlaying()) {
                    mp.stop();
                }
                mp = MediaPlayer.create(getApplicationContext(), R.raw.inter_prim);
                mp.start();
            }
        });



       some buttons....

我在每个片段中添加了以下内容:

public void playImageSound(){
            imagePlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mp != null && mp.isPlaying()) {
                        mp.stop();
                    }
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.soundx);
                    mp.start();
                }
            });
        }
fragmentx.playImageSound();
这是每一个片段的一次创造:

public void playImageSound(){
            imagePlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mp != null && mp.isPlaying()) {
                        mp.stop();
                    }
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.soundx);
                    mp.start();
                }
            });
        }
fragmentx.playImageSound();

你能包括堆栈跟踪吗?强制关闭意味着logcat中必须有堆栈跟踪对不起,但是在Android Studio中我在哪里可以找到堆栈跟踪?我不使用Android Studio,但也必须有logcat!好的,错误在您的
onCreateView
方法中,您能告诉我吗?特别是这一行:Gehortestet_intervaller初学者。java:292