button1.setOnClickListener(本);导致NullPointer

时间:2014-02-17 18:13:35

标签: java android android-fragments onclicklistener

我被要求开一个新问题。

我有一个显示片段的滑动菜单。其中一个片段是一组按下按钮时播放音频的按钮。

按钮的OnClickListener导致logcat中出现NullPointer错误并导致应用程序崩溃。谁能告诉我为什么?这些按钮是通过与片段相关的正确xml引用的,因此我不确定导致错误的是什么。

这些行导致了问题。

 button1.setOnClickListener(this);
 button2.setOnClickListener(this);

SoundsFragment.java ,其上包含按钮和代码。

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;


public class SoundsFragment extends Fragment implements OnClickListener{

    public SoundsFragment(){}

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

        View rootView = inflater.inflate(R.layout.fragment_sounds, container, false);

        return rootView;
    }

    private MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.fragment_sounds);
     setVolumeControlStream(AudioManager.STREAM_MUSIC);
     Button button1=(Button)findViewById(R.id.button_1);
     Button button2=(Button)findViewById(R.id.button_2);
     button1.setOnClickListener(this);
     button2.setOnClickListener(this);
    }

    private Button findViewById(int button1) {
        // TODO Auto-generated method stub
        return null;
    }

    private void setVolumeControlStream(int streamMusic) {
        // TODO Auto-generated method stub

    }

    private void setContentView(int activityMain) {
        // TODO Auto-generated method stub

    }

    public void onClick(View v) {
     int resId;
     switch (v.getId()) {
     case R.id.button_1:
       resId = R.raw.a;
       break;
     case R.id.button_2:
        resId = R.raw.b;
       break;
     default:
       resId = R.raw.a;
       break;
     }
     // Release any resources from previous MediaPlayer
     if (mp != null) {
        mp.release();
     }
     // Create a new MediaPlayer to play this sound
     mp = MediaPlayer.create(getActivity(), resId);
     mp.start();
    }


    @Override
    public void onDestroy() {
      if(null!=mp){
     mp.release();
      }
      super.onDestroy();
    }
}

fragment_sounds.xml,它有按钮和ID。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/sounds_fragment">

    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="22dp"
        android:layout_marginTop="36dp"
        android:text="Play audio a.mp3" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_1"
        android:layout_below="@+id/button_1"
        android:text="Play audio b.mp3" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:3)

更改为

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

    View rootView = inflater.inflate(R.layout.fragment_sounds, container, false);
    Button button1=(Button)rootView.findViewById(R.id.button_1);
    Button button2=(Button)rootView.findViewById(R.id.button_2);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this); 
    return rootView;
}

删除此

   private Button findViewById(int button1) {
    // TODO Auto-generated method stub
    return null;
  } 

 private void setContentView(int activityMain) {
    // TODO Auto-generated method stub

}

  setContentView(R.layout.fragment_sounds);
onCreate

中的

答案 1 :(得分:0)

是的,button1为空,这就是您获得NullPointerException的原因。

答案 2 :(得分:0)

您忘记在@Override ...

之前添加符号onClick(View v)
@Override
public void onClick(View v) {