Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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 在Android的片段中添加onTouchListener on按钮_Java_Android_Android Fragments_Android Fragmentactivity_Ontouchlistener - Fatal编程技术网

Java 在Android的片段中添加onTouchListener on按钮

Java 在Android的片段中添加onTouchListener on按钮,java,android,android-fragments,android-fragmentactivity,ontouchlistener,Java,Android,Android Fragments,Android Fragmentactivity,Ontouchlistener,我试图在片段中的按钮上添加onTouchListner。这段代码在主活动中运行良好,但在片段上运行不好。只要按下按钮,我想做的就是播放特定的声音文件。任何帮助都将不胜感激 代码是 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { message1(); return inflater.inflate(R.layo

我试图在片段中的按钮上添加onTouchListner。这段代码在主活动中运行良好,但在片段上运行不好。只要按下按钮,我想做的就是播放特定的声音文件。任何帮助都将不胜感激

代码是

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

    message1();

return inflater.inflate(R.layout.fragment_fragment_a, container, false);


}

public  void message1() {
    // TODO Auto-generated method stub


         ImageButton one1 = (ImageButton) getView().findViewById(R.id.imageButton1);


         one1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int resID =   R.raw.a;
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //mp.setLooping(true);

                    if (mp != null) {
                         mp.release();
                      }
                      // Create a new MediaPlayer to play this sound
                      mp = MediaPlayer.create( getActivity(), resID);
                      mp.start();
                    break;

                case MotionEvent.ACTION_UP:

                    mp.stop();

                    break;
                }

                return true;

            }

}))

调用
message1()
时,布局未充气。因此,
findViewById
无法找到您的按钮,因为它在视图层次结构中不存在

请尝试以下操作:

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

    View view =  inflater.inflate(R.layout.fragment_fragment_a, container, false)
    ImageButton one1 = (ImageButton) view.findViewById(R.id.imageButton1);
    one1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int resID =   R.raw.a;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //mp.setLooping(true);

                if (mp != null) {
                     mp.release();
                  }
                  // Create a new MediaPlayer to play this sound
                  mp = MediaPlayer.create( getActivity(), resID);
                  mp.start();
                break;

            case MotionEvent.ACTION_UP:
                mp.stop();
                break;
            }
            return true;
        }
    });
    return view;


}

调试后你检查过了吗?我是说你有什么错误吗?太好了。。。谢谢你的解决方案