有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java安卓开发线程问题

我正在eclipse中制作一个安卓应用程序,我的问题是,每次我在模拟器上运行它时,它都会在5秒钟后关闭,并给我一条消息“不幸的是,‘应用程序名称’已经停止工作”。我想这是因为线程会显示我放在drawable hdpi文件夹中的图片,因为线程会显示图片5秒钟,然后启动程序。非常感谢您的帮助

package com.thenewboston.travis;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;

public class Splash extends Activity{

@Override
protected void onCreate(Bundle t) {
    // TODO Auto-generated method stub
    super.onCreate(t);
    setContentView(R.layout.splash);
 new Thread(){
        public void run(){
            try{
            sleep(5000);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                Intent openStartingPoint= new Intent("com.thenewboston.travis.STARTINGPOINT");
                startActivity(openStartingPoint);
            }
        }
    }.start(); }}

Android清单代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.thenewboston.travis"
安卓:versionCode="1"
安卓:versionName="1.0" >

<uses-sdk
    安卓:minSdkVersion="8"
    安卓:targetSdkVersion="18" />

<application
    安卓:allowBackup="true"
    安卓:icon="@drawable/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name="com.thenewboston.travis.Splash"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        安卓:name="com.thenewboston.travis.startingPoint"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

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


</application>

</manifest>

这是我的启动文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" 
安卓:background="@drawable/yaron_dynamics">


</LinearLayout>

共 (2) 个答案

  1. # 1 楼答案

    不能在与UI线程不同的线程中startActivity

  2. # 2 楼答案

    无法从后台线程启动Activity。使用Handler代替线程:

    Runnable r = new Runnable() {
       public void run() {
           Intent i = new Intent("com.thenewboston.travis.STARTINGPOINT");
           startActivity(i);
       }
    };
    
    new Handler().postDelayed(r, 5000);