1

我无法从线程开始活动。我想在 2 秒后开始一项活动。但它给出了错误 - 应用程序意外停止。

这是我要从中运行线程的活动的代码。

package com.pack.prog;

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

public class Splash extends Activity {

    MediaPlayer mPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
                    LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.splash);

            mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments);
            mPlayer.start();

            Thread thread = new Thread() {
                public void run() {

                    try {
                        sleep(2000);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Toast.makeText(Splash.this, e.toString(),
                                Toast.LENGTH_LONG).show();
                    } finally {
                        Intent i = new Intent("com.pack.prog.StartingMenu");
                        startActivity(i);
                    }

                };
            };
            thread.start();

        } catch (Exception e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPause() {

        try {
            super.onPause();
            if (mPlayer.isPlaying()) {
                mPlayer.release();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onBackPressed() {
        try {
            super.onBackPressed();
            if (mPlayer.isPlaying()) {
                mPlayer.release();
            }
        } catch (Exception w) {
            Toast.makeText(this, w.toString(), Toast.LENGTH_LONG).show();
        } finally {
            finish();
        }
    }

}

这是清单文件。

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".StartingMenu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.pack.prog.STARTINGMENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


    </application>

</manifest>

我认为只有清单存在一些问题。

4

6 回答 6

2

我认为动作名称不同"com.pack.prog.StartingMenu"并且在清单中 "com.pack.prog.STARTINGMENU"

并在 UI 线程中执行此操作.....

 

  mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments);
  mPlayer.start();
new Handler().postDelayed(new Runnable() {

                public void run() {

                    Intent i = new Intent("com.pack.prog.STARTINGMENU"); //<--- what ever which is right
                    startActivity(i); 
                }
            }, 2000);
于 2012-06-15T07:20:51.147 回答
2

试试这个

  mediaplayer = MediaPlayer.create(getBaseContext(), R.raw.happy_moments);
    mediaplayer.start();                                                       
        MyThread myThread=new MyThread();
        myThread.start();

}                
class MyThread extends Thread
{
    public void run(){
        try{                                      
            Thread.sleep(2000);                
        }                              
        catch(Exception ex){

            Log.e("Welcome Exception :",ex.toString());
        }                      
        mHandler.sendEmptyMessage(0);     
    }                   
}
Handler mHandler=new Handler(){
    public void handleMessage(Message msg){
        super.handleMessage(msg);                

        if ( )
        {    
            MyThread myThread=new MyThread();
            myThread.start();
        }
        else
        {
     Intent i=new Intent(firstactivity.this,secondactivity.class);
            startActivity(i);
            finish();
        }
    }
};

}

于 2012-06-15T09:58:16.490 回答
1

首先,您的操作不匹配 Intent i = new Intent("com.pack.prog.StartingMenu"); ,并且在清单中是<action android:name="com.pack.prog.STARTINGMENU" />

第二个 严重问题

您可以在线程中触摸 UI 部分,您可以通过将它们封闭在线程内与 UI 进行交互runOnUiThread

所以把你的代码改成这个。

Thread thread = new Thread() {
            public void run() {

                try {
                    sleep(2000);
                } catch (Exception e) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(Splash.this, e.toString(),
                                    Toast.LENGTH_LONG).show();

                        }
                    });

                } finally {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Intent i = new Intent("com.pack.prog.StartingMenu");
                            startActivity(i);

                        }
                    });

                }

            };
        };
        thread.start();
于 2012-06-15T07:21:39.370 回答
0

应该使用

runOnUiThread(new Runnable() {
    public void run() {
                    try {
                        sleep(2000);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Toast.makeText(Splash.this, e.toString(),
                                Toast.LENGTH_LONG).show();
                    } finally {
                        Intent i = new Intent("com.pack.prog.StartingMenu");
                        startActivity(i);
                    }
    }
});
于 2012-06-15T07:19:39.603 回答
0

你不能从工作线程执行与 UI 相关的东西

于 2012-06-15T07:20:41.747 回答
0

我更喜欢使用Timer. 检查以下实现:

Timer t =new Timer();
t.schedule(new TimerTask() {

    @Override
    public void run() {
        Intent i = new Intent("com.pack.prog.StartingMenu");
        startActivity(i);
    }
}, 2000); 
于 2012-06-15T07:25:21.543 回答