我在扩展YouTubePlayerFragment时收到NullPointerException。
PFB我的代码:
public class VideoFragment extends YouTubePlayerFragment {
private OnFragmentInteractionListener_videoFragment mListener;
YouTubePlayer myPlayer ;
public static VideoFragment newInstance(String url) {
VideoFragment f = new VideoFragment();
Bundle b = new Bundle();
b.putString("url", url);
f.setArguments(b);
f.init();
return f;
}
private void init(){
initialize(DeveloperKey.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
myPlayer=youTubePlayer;
if(!b){
myPlayer.cueVideo(getArguments().getString("url"));
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.i("Unable to initialize the player","the result");
Dialog errorDialog = youTubeInitializationResult.getErrorDialog(getActivity(),0);
errorDialog.show();
}
});
}
public VideoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
String url = getArguments().getString("url");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_video_standard, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction_videoFragment(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener_videoFragment) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener_videoFragment {
// TODO: Update argument type and name
public void onFragmentInteraction_videoFragment(Uri uri);
}
}
PFB错误:
java.lang.NullPointerException
at com.google.android.youtube.player.YouTubePlayerFragment.onStart(Unknown Source)
at android.app.Fragment.performStart(Fragment.java:1719)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
以及我在MainActivity中初始化的方式:
VideoFragment vf =VideoFragment.newInstance(testArray[id]); getFragmentManager().beginTransaction().replace(R.id.content_frame,vf).commit() ;