I'm trying to use YouTube API in my Android app. First - if I've added lib to my project should something in values appear?
Second one is here, my error:
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 (I've took it to test from one site)
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.krasiniak.krachapp_beta"
android:versionCode="2"
android:versionName="1.1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<usues-library android:name="com.google.android.youtube.player"/>
<activity
android:name="pl.krasiniak.krachapp_beta.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="pl.krasiniak.krachapp_beta.Schedule"
android:label="Plan Lekcji"
android:parentActivityName="pl.krasiniak.krachapp_beta.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="pl.krasiniak.krachapp_beta.MainActivity" />
</activity>
<activity
android:name="pl.krasiniak.krachapp_beta.TimeTable"
android:label="Tablica"
android:parentActivityName="pl.krasiniak.krachapp_beta.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="pl.krasiniak.krachapp_beta.MainActivity" />
</activity>
<activity
android:name="pl.krasiniak.krachapp_beta.AboutApp"
android:label="O Aplikacji"
android:parentActivityName="pl.krasiniak.krachapp_beta.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="pl.krasiniak.krachapp_beta.MainActivity" />
</activity>
<activity
android:name="pl.krasiniak.krachapp_beta.Changes"
android:label="@string/title_activity_changes"
android:parentActivityName="pl.krasiniak.krachapp_beta.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="pl.krasiniak.krachapp_beta.MainActivity" />
</activity>
<activity
android:name="pl.krasiniak.krachapp_beta.Studio"
android:label="@string/title_activity_studio"
android:parentActivityName="pl.krasiniak.krachapp_beta.MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="pl.krasiniak.krachapp_beta.MainActivity" />
</activity>
</application>
I've read many topics on stack becouse this is common error but none solution worked for me. Greetings!
EDIT: Here is my activity_studio.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello YouTube API"
android:layout_margin="12dip"/>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dip"/>
And it says it's error becouse:
java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
But it still isn't clear for me
Caused by: java.lang.NullPointerException
at pl.krasiniak.krachapp_beta.Studio.onCreate(Studio.java:25)
I guess Studio.java line 25 is youTubePlayerView.initialize(API_KEY, this)
YouTubePlayerView
is null. You should check the id of the view in xml
Edit:
setContentView(R.layout.activity_main);
But you have
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
in activity_studio.xml
SO change to
setContentView(R.layout.activity_studio);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With