main java.lang.RuntimeException:无法启动活动ComponentInfo

我正在尝试在我的Android应用中使用YouTube API。 首先 – 如果我在我的项目中添加了lib,那么值是否会出现值?

第二个在这里,我的错误:

logcat的:

E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.krasiniak.krachapp_beta/pl.krasiniak.krachapp_beta.Studio}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) at android.app.ActivityThread.access$600(ActivityThread.java:132) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4575) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at pl.krasiniak.krachapp_beta.Studio.onCreate(Studio.java:25) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)             at android.app.ActivityThread.access$600(ActivityThread.java:132)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:4575)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)             at dalvik.system.NativeStart.main(Native Method) 

Studio.java(我从一个网站上测试过它)

  package pl.krasiniak.krachapp_beta; 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.YouTubePlayerView; import com.google.android.youtube.player.YouTubePlayer.Provider; import android.os.Bundle; import android.widget.Toast; public class Studio extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{ public static final String API_KEY = "blabla"; public static final String VIDEO_ID = "o7VVHhK9zf0"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtube_view); youTubePlayerView.initialize(API_KEY, this); } @Override public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) { Toast.makeText(getApplicationContext(), "onInitializationFailure()", Toast.LENGTH_LONG).show(); } @Override public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { player.cueVideo(VIDEO_ID); } } } 

的Manifest.xml

                              

我已经在堆栈上阅读了很多主题,因为这是常见的错误,但没有一个解决方案适合我。 问候!

编辑:这是我的activity_studio.xml

    

它说这是错误因为:

 java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context. 

但对我来说仍然不太清楚

  Caused by: java.lang.NullPointerException at pl.krasiniak.krachapp_beta.Studio.onCreate(Studio.java:25) 

我猜Studio.java第25行是youTubePlayerView.initialize(API_KEY, this)

YouTubePlayerView为空。 您应该在xml中检查视图的ID

编辑:

  setContentView(R.layout.activity_main); 

但是你有

   

activity_studio.xml

所以改为

 setContentView(R.layout.activity_studio);