目前我正在编写扩展自定义 SherlockFragment 的片段,该扩展扩展了 YouTubePlayerSupportFragment。不幸的是,应用程序崩溃了。
代码:
public static class YouTubeVideo extends SherlockYouTubeFragment
implements YouTubePlayer.OnInitializedListener{
private TextView titleview;
private TextView descview;
private TextView countview;
private TextView timeview;
private Bundle data;
private YouTubePlayer ytp;
private String title;
private String id;
private String desc;
private String count;
private String time;
private int timeint;
private int timem;
private int times;
private Tracker myTracker;
private ShareActionProvider mShareActionProvider;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
data = getArguments();
return inflater.inflate(R.layout.youtubeview, container, false);
}
@Override
public void onStart() {
titleview = (TextView) getView().findViewById(R.id.title);
descview = (TextView) getView().findViewById(R.id.descriptiontext);
countview = (TextView) getView().findViewById(R.id.viewcounttext);
timeview = (TextView) getView().findViewById(R.id.timetext);
title = data.getString("title");
id = data.getString("id");
desc = data.getString("description");
count = data.getString("viewcount");
time = data.getString("time");
EasyTracker.getInstance().setContext(this.getActivity());
myTracker = EasyTracker.getInstance().getTracker();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
fragment.initialize(ID, this);
fragmentTransaction.add(R.id.youtubeplayer, fragment);
fragmentTransaction.commit();
titleview.setText(title);
descview.setText(desc);
countview.setText(count);
try {
timeint = Integer.parseInt(time);
timem = timeint/60;
times = timeint - (timem*60);
} catch(NumberFormatException nfe) {
// Handle parse error.
}
if (times<10){
timeview.setText(String.valueOf(timem)+":0"+String.valueOf(times));
}
else{
timeview.setText(String.valueOf(timem)+":"+String.valueOf(times));
}
super.onStart();
}
@Override
public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
Toast.makeText(this.getActivity(), "Uruchamianie filmu nie powiodło się", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,boolean wasrestored) {
ytp = player;
player.addFullscreenControlFlag(4);
if(ytp !=null){
ytp.loadVideo(id);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.video_menu, menu);
mShareActionProvider = (ShareActionProvider)menu.findItem(R.id.share).getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
myTracker.trackEvent("videoview", "share", title + id, (long) 3);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, title + " - http://ignastv.tk/id/" + id);
sendIntent.setType("text/plain");
mShareActionProvider.setShareIntent(sendIntent);
return sendIntent;
}
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if(itemId == R.id.vidsite){
myTracker.trackEvent("videoview", "open-in", title + id, (long) 3);
Intent govideo = new Intent(Intent.ACTION_VIEW);
govideo.setData(Uri.parse(id));
startActivity(govideo);
return true;}
else {
return super.onOptionsItemSelected(item);
}
}
}
最有趣的是 LogCat 显示有 NullPointerException 发生在一行中:
super.onStart();
这是LogCat:
04-27 20:19:30.055: E/AndroidRuntime(14706): FATAL EXCEPTION: main
04-27 20:19:30.055: E/AndroidRuntime(14706): java.lang.NullPointerException
04-27 20:19:30.055: E/AndroidRuntime(14706): at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
04-27 20:19:30.055: E/AndroidRuntime(14706): at com.app.app.MainActivity$YouTubeVideo.onStart(MainActivity.java:519)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.support.v4.app.Fragment.performStart(Fragment.java:1332)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.os.Handler.handleCallback(Handler.java:725)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.os.Looper.loop(Looper.java:137)
04-27 20:19:30.055: E/AndroidRuntime(14706): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-27 20:19:30.055: E/AndroidRuntime(14706): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 20:19:30.055: E/AndroidRuntime(14706): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 20:19:30.055: E/AndroidRuntime(14706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 20:19:30.055: E/AndroidRuntime(14706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 20:19:30.055: E/AndroidRuntime(14706): at dalvik.system.NativeStart.main(Native Method)
这段代码有什么问题?