简体   繁体   English

Android开发线程问题

[英]Android development thread issue

I am making an android app in eclipse my problem is that every time I run it on my emulator it closes after 5 seconds and gives me a message “Unfortunately, 'app name' has stopped working”. 我在eclipse中制作一个Android应用程序我的问题是每次我在我的模拟器上运行它在5秒后关闭并给我一条消息“不幸的是,'app name'已停止工作”。 I think this is due to the thread that brings up a picture I put in the drawable-hdpi folder because the thread is meant to bring up the picture for 5 seconds then start the program. 我认为这是由于线程带来了我放在drawable-hdpi文件夹中的图片,因为该线程意味着将图片调出5秒然后启动程序。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

package com.thenewboston.travis;

import android.app.Activity;
import android.content.Intent;
import android.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(); }}

The Android Manifest Code Android清单代码

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.thenewboston.travis.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="com.thenewboston.travis.startingPoint"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


</application>

</manifest>

And this is my splash file 这是我的启动文件

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


</LinearLayout>

您不能在与UI线程不同的线程中启动startActivity

You can't launch an Activity from a background thread. 您无法从后台线程启动Activity Instead of a thread, use a Handler : 而不是一个线程,使用一个Handler

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

new Handler().postDelayed(r, 5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM