应用程序保持强制关闭?

时间:2011-08-07 03:30:27

标签: java android eclipse

  

可能重复:
  Having Trouble with my Splash Screen

最近我尝试添加一个启动画面,我觉得我搞砸了,因为我的应用程序一直强行关闭。

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) {
            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() {
        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));
                }

            });
        }
    }

清单:

<?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 23:27:56.259:ERROR / dalvikvm(10851):无法禁用pid 10851的核心文件生成,errno = 1 08-06 23:27:56.588:ERROR /(5531):该文件不是ASF文件。 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):致命异常:主要 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):java.lang.RuntimeException:无法启动活动ComponentInfo {com.Dragon_Fruit / com.Dragon_Fruit.DragonFruitActivity}:java.lang.NullPointerException 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.ActivityThread.access $ 2500(ActivityThread.java:129) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2107) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.os.Handler.dispatchMessage(Handler.java:99) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.os.Looper.loop(Looper.java:143) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.ActivityThread.main(ActivityThread.java:4701) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):at java.lang.reflect.Method.invokeNative(Native Method) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):at java.lang.reflect.Method.invoke(Method.java:521) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):at dalvik.system.NativeStart.main(Native Method) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):引起:java.lang.NullPointerException 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):at com.Dragon_Fruit.DragonFruitActivity.onStart(DragonFruitActivity.java:48) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.Activity.performStart(Activity.java:3781) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2710) 08-06 23:27:56.689:ERROR / AndroidRuntime(10851):... 11更多

2 个答案:

答案 0 :(得分:3)

很难说这是否真的导致异常,但你肯定不能从一个线程调用setContentView()。所有UI操作都必须从主(UI)线程完成。

您可以使用Handler及其postDelayed()方法在指定的超时后更改视图。 Handler类已记录在案here

答案 1 :(得分:0)

logcat显示的错误是android系统无法在“splash”布局中找到“playbutton”。

看起来您正试图在AndroidManifest.xml中定义按钮。您应该在布局中定义它们,如下所述: http://developer.android.com/guide/topics/ui/declaring-layout.html 如果playbutton和settingsbutton是实际活动,我建议将它们大写以匹配java约定。 你应该删除:

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

您在第48行关闭力量的原因是因为您在将其添加到活动之前尝试访问您的播放按钮,您将在计时器中稍后执行此操作。尝试移动:

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

        });

进入你的计时器代码,以便它出现在DragonFruitActivity.this.setContentView(R.layout.main)之后。

最后,您的上一个问题是您不应该修改工作线程上的UI。有关更多信息和解决方案,请参阅此帖 http://android-developers.blogspot.com/2009/05/painless-threading.html