我的启动画面有问题

时间:2011-08-07 01:19:56

标签: android eclipse

所以我刚刚开始设置一个显示5秒的启动画面,然后转到我的主菜单。我认为我在这个过程中标记了一些错误,现在我的应用程序正在关闭。我很确定这是我的活动和我的清单的问题。

DragonFruitActivity.java:

 package com.Dragon_Fruit;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class DragonFruitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread logoTimer = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                    DragonFruitActivity.this.setContentView(R.layout.main);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally {
                    finish();
                }
            }
        };
        logoTimer.start();

        // ***BUTTON SOUND***//
        final MediaPlayer buttonSound = MediaPlayer.create(
                DragonFruitActivity.this, R.raw.button_click);

        ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton);
        playbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                arg0.setBackgroundResource(R.drawable.playbuttonselected);
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent(DragonFruitActivity.this,
                        playbutton.class));
            }

        });
        ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton);
        settingsbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent(DragonFruitActivity.this,
                        settingsbutton.class));
            }

        });
    }
}

Dragon Fruit Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Dragon_Fruit" 
    android:versionCode="1" 
    android:versionName="1.0"
    android:installLocation="preferExternal">


    <application android:icon="@drawable/icon" 
                 android:label="@string/app_name"
                 android:debuggable="true">

        <activity android:name=".DragonFruitActivity" 
                  android:label="@string/app_name"
                  android:screenOrientation="landscape" 
                  android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".playbutton" 
                  android:label="@string/app_name"
                  android:screenOrientation="landscape" 
                  android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="com.Dragon_Fruit.PLAYBUTTON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".settingsbutton" 
                  android:label="@string/app_name"
                  android:screenOrientation="landscape" 
                  android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="com.Dragon_Fruit.SETTINGSBUTTON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="9" />
</manifest>

logcat的:

08-06 22:46:27.942: ERROR/dalvikvm(9896): could not disable core file generation for pid 9896, errno=1
08-06 22:46:28.271: ERROR/(5531): This file is not ASF file.
08-06 22:46:28.364: ERROR/AndroidRuntime(9896): FATAL EXCEPTION: main
08-06 22:46:28.364: ERROR/AndroidRuntime(9896): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Dragon_Fruit/com.Dragon_Fruit.DragonFruitActivity}: java.lang.NullPointerException
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.ActivityThread.access$2500(ActivityThread.java:129)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.os.Looper.loop(Looper.java:143)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.ActivityThread.main(ActivityThread.java:4701)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at java.lang.reflect.Method.invoke(Method.java:521)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at dalvik.system.NativeStart.main(Native Method)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896): Caused by: java.lang.NullPointerException
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at com.Dragon_Fruit.DragonFruitActivity.onCreate(DragonFruitActivity.java:44)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
08-06 22:46:28.364: ERROR/AndroidRuntime(9896):     ... 11 more

2 个答案:

答案 0 :(得分:1)

在你的帖子中,尝试更改:

 Intent DragonFruitActivityIntent = new Intent("main");

为:

 Intent DragonFruitActivityIntent = new Intent(DragonFruitActivity.this, NameOfActivityToStart.class);

或者,如果您想更改布局而不是开始新活动,请删除:

Intent DragonFruitActivityIntent = new Intent("main");
startActivity(DragonFruitActivityIntent);

而是写:

DragonFruitActivity.this.setContentView(R.layout.main);

顺便说一句,如果不是在5秒钟内休眠,那么线程会做一些有用的事情,例如为你的应用加载一些数据 - 否则它只会减慢你对应用的访问速度。

修改:尝试将所有代码从onCreate()移到onStart(),所以就像这样:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);        
   setContentView(R.layout.splash);
}

@Override
public void onStart() {
   super.onStart()
   Thread logoTimer = new Thread() {
   ... // the rest of your code here
}

在完全形成之前,您可能会遇到访问用户界面的问题。

答案 1 :(得分:0)

正如你的日志猫所暗示的那样......

at com.Dragon_Fruit.DragonFruitActivity.onCreate(DragonFruitActivity.java:44)

您的DragonFruitActivity文件存在一些问题。在OnCreate回调中的第44行..

我想删除它可以解决NullpointerException的问题。