Android应用程序运行startActivity(使用YouTubeAPI)后崩溃



我刚刚开始Android应用程序开发的世界,我有很多乐趣,但我现在卡住了。如果有人能帮助我,我真的很感激。

基本上我已经创建了一个应用程序,当它开始加载一个启动屏幕,然后在4秒后完成并移动到MainActivity。一切都很顺利,直到我移动了一个YouTubeAPI YouTubePlayerView并开始播放。(注意:我已经将youtubeandroidplayerapi.jar添加到lib文件夹中,然后右键单击添加到构建路径中…也清理和重建)

现在当启动画面结束时,我得到消息"不幸的是,我的应用程序已经停止了。"

查看logcat报告:

    08-07 02:12:39.420: E/AndroidRuntime(13704): FATAL EXCEPTION: main
08-07 02:12:39.420: E/AndroidRuntime(13704): Process: com.example.myapp, PID: 13704
08-07 02:12:39.420: E/AndroidRuntime(13704): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: java.lang.NullPointerException
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.os.Looper.loop(Looper.java:136)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.ActivityThread.main(ActivityThread.java:5017)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at java.lang.reflect.Method.invokeNative(Native Method)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at java.lang.reflect.Method.invoke(Method.java:515)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at dalvik.system.NativeStart.main(Native Method)
08-07 02:12:39.420: E/AndroidRuntime(13704): Caused by: java.lang.NullPointerException
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.Activity.findViewById(Activity.java:1884)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at com.example.myapp.MainActivity.<init>(MainActivity.java:29)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at java.lang.Class.newInstanceImpl(Native Method)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at java.lang.Class.newInstance(Class.java:1208)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-07 02:12:39.420: E/AndroidRuntime(13704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)

启动代码:

public class SplashScreen extends Activity {
    private static int TIMER_LIMIT = 4000; //Set the splash screen timer (4000 = 4 seconds)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splashscreen);
        //here the code is going to start the timer. note two parameters
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                //set-up to move the operation to the MainActivity class
                startActivity(i);
                finish();
            }
        }, TIMER_LIMIT);
    }
}

MainActivity

public class MainActivity extends YouTubeBaseActivity
     implements YouTubePlayer.OnInitializedListener,OnEditorActionListener {
    private Spinner spin = (Spinner) findViewById(R.id.spinner1);
    private YouTubePlayerView ytpv;
    private YouTubePlayer ytp;
    /*START: Standard Activity Functions*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnSpinnerItemSelection();//setup listener on spinner
        addListenerOnButton();//setup listener on button
        ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
        ytpv.initialize("My API KEY", this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult arg1) {
        Toast.makeText(this, "Initialization Fail", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasrestored) {
        ytp = player;
        Toast.makeText(this, "Initialization Success", Toast.LENGTH_LONG).show();
    }

activity_main.xml(全)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    >
        <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.38" >
            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubeplayer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"> 
            </com.google.android.youtube.player.YouTubePlayerView>
    </FrameLayout>
        <LinearLayout
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.04"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/txtVideo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/video_select"
                android:textAppearance="?android:attr/textAppearanceSmall" />
            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:entries="@array/list_videos"
                android:layout_marginBottom="10dp"
                android:prompt="@string/choose_video" />
            <TextView
                android:id="@+id/txtTimer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/timer"
                android:textAppearance="?android:attr/textAppearanceSmall" />
            <Chronometer
                android:id="@+id/chronometer1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:textSize="45sp" />
            <TextView
                android:id="@+id/txtMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="70dp"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <Button
                android:id="@+id/btnStartQuit"
                android:text="@string/btnStartQuit" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:layout_marginBottom="5dp"/>

        </LinearLayout>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />
    <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" >
        <!-- Splash screen -->
        <activity
            android:name="com.example.myapp.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Main activity -->
        <activity
            android:name="com.example.myapp.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape" 
            android:configChanges="keyboardHidden|orientation">
        </activity>
    </application>

</manifest>

您正在初始化

private Spinner spin = (Spinner) findViewById(R.id.spinner1);

初始化必须在

  • OnCreate ();
  • onResume ();
  • onStart ();

因为之前活动上下文不存在,所以

    private Spinner spin;
    private YouTubePlayerView ytpv;
    private YouTubePlayer ytp;
    /*START: Standard Activity Functions*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spin = (Spinner) findViewById(R.id.spinner1);//// code added
        addListenerOnSpinnerItemSelection();//setup listener on spinner
        addListenerOnButton();//setup listener on button
        ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
        ytpv.initialize("My API KEY", this);
    }

相关内容

  • 没有找到相关文章

最新更新