我正在尝试在一个片段中使用Youtube播放器支持片段,但应用程序总是崩溃(NullPointerException),而我没有能够找到任何类似的帖子来解决它。
我已经导入了import android.support.v4.app.Fragment,所以那不应该是问题。
这是我的片段类的样子:
package com.example.activitydetector;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayerView;
import systemManager.SystemManager;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GuidelinesFragment extends YouTubePlayerSupportFragment {
SystemManager sm;
YouTubePlayerView youTubeView;
String URL_VIDEO = "CaA-k1l0xa4";
String KEY_DEVELOPER = "AIzaSyBIIs0u0NXhsZguv8nCNvSzUmflTt7K1Ek";
public GuidelinesFragment() {
super();
// TODO Auto-generated constructor stub
}
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.youtube, container, false);
YouTubePlayerSupportFragment youTubePlayerSupportFragment = (YouTubePlayerSupportFragment) getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerSupportFragment.initialize(KEY_DEVELOPER, new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
// TODO Auto-generated method stub
arg1.cueVideo(URL_VIDEO);
}
@Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
return view;
}
}
这是我完全简单的“Youtube”布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtubeplayerfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这是日志显示的错误:
11-30 16:33:56.419: W/dalvikvm(19375): threadid=1: thread exiting with uncaught exception (group=0x40f14258)
11-30 16:33:56.423: E/AndroidRuntime(19375): FATAL EXCEPTION: main
11-30 16:33:56.423: E/AndroidRuntime(19375): java.lang.NullPointerException
11-30 16:33:56.423: E/AndroidRuntime(19375): at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.os.Handler.handleCallback(Handler.java:605)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.os.Looper.loop(Looper.java:137)
11-30 16:33:56.423: E/AndroidRuntime(19375): at android.app.ActivityThread.main(ActivityThread.java:4645)
11-30 16:33:56.423: E/AndroidRuntime(19375): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 16:33:56.423: E/AndroidRuntime(19375): at java.lang.reflect.Method.invoke(Method.java:511)
11-30 16:33:56.423: E/AndroidRuntime(19375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-30 16:33:56.423: E/AndroidRuntime(19375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-30 16:33:56.423: E/AndroidRuntime(19375): at dalvik.system.NativeStart.main(Native Method)
非常感谢任何帮助或提示。我已经浪费了大约4个小时,但没有运气。