[英]Fragment in Android for MediaPlayer getting null pointers?
I have this code: 我有以下代码:
Button one = (Button)getView().findViewById(R.id.button_audio);
final MediaPlayer mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.idontlike);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
I'm getting this error: 我收到此错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
You need to search in the fragment layout so try the following 您需要搜索片段布局,因此请尝试以下操作
View myView= inflater.inflate(R.layout.fragment_main, container, false);
Button one = (Button) myView.findViewById(R.id.button);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.