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

时间:2014-01-05 18:56:47

标签: java android api youtube

我正在尝试在我的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

<?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>

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

编辑:这是我的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"/>

它说这是错误的:

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

但我现在还不清楚

1 个答案:

答案 0 :(得分:11)

 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);

但你有

  <com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
activity_studio.xml

中的

SO改为

setContentView(R.layout.activity_studio);