最近学校让做个android音乐播放器,我跟着论坛的教学做到这里时会出现闪退的问题,不知道怎么解决,求助(我是小白,如果可以请说详细些,谢谢啦)
放置fragment的activity没有实现OnMainFragmentInteractionListener这个接口。
楼主如果不需要用到这个接口, 就把@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnMainFragmentInteractionListener) {
mListener = (OnMainFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
but = (Button)rootView.findViewById(myMusicButton);
这种写法实在不提倡
这个是出错的代码
package com.example.ycxh.atry;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import static com.example.ycxh.atry.R.id.myMusicButton;
public class MainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
but = (Button)rootView.findViewById(myMusicButton); //切换至我的音乐Fragment
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onMainFragmentInteraction(AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT);
}
});
return rootView;
}
public interface OnMainFragmentInteractionListener {
// TODO: Update argument type and name
// void onFragmentInteraction(int message);
void onMainFragmentInteraction(int message);
}
空指针异常一般都是最容易解决的。
你看看报的错误是对应那一行的代码,这一行代码中的那个对象是空指针异常的原因。
检查这个对象为空的原因以及该对象在上下文的使用。
mListener.onMainFragmentInteraction(AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT);
mListener这个变量没定义吧?多看几遍
package com.example.ycxh.atry;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import static com.example.ycxh.atry.R.id.myMusicButton;
/**
create an instance of this fragment.
*/
public class MainFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private Button but;
private OnMainFragmentInteractionListener mListener;
public MainFragment() {
// Required empty public constructor
}
/**
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
but = (Button)rootView.findViewById(myMusicButton); //切换至我的音乐Fragment
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onMainFragmentInteraction(AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT);
}
});
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(int message) {
if (mListener != null) {
mListener.onMainFragmentInteraction(message);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnMainFragmentInteractionListener) {
mListener = (OnMainFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
Communicating with Other Fragments for more information.
*/
public interface OnMainFragmentInteractionListener {
// TODO: Update argument type and name
// void onFragmentInteraction(int message);
void onMainFragmentInteraction(int message);
}
}