使用线程启动活动

时间:2013-09-15 09:49:26

标签: android eclipse multithreading

就像标题告诉我在我的Android Galaxy Ace S5830i上运行这个应用程序时遇到问题。

  1. 我刚刚在eclipse中创建了一个新的Android应用程序。
  2. 在First中添加了Splash.java - > src - > com.example.first - > Splash.java
  3. 添加了布局res - > layout - > splash.xml(它基本上只是eclipse设置的activity_fullscreen.xml的副本,只是按钮已被删除)
  4. 在清单
  5. 中添加了一些代码

    每当我在设备上启动应用程序时,它都会崩溃 "应用程序First(进程com.example.first)意外停止。请再试一次。"

    我可以告诉你,启动启动时会启动启动活动,但在尝试启动其他活动时会崩溃。

    Splash.java

        package com.example.first;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    
    public class Splash extends Activity{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent start = new Intent("com.example.first.FullscreenActivity");
                    startActivity(start);
                }
            }
        };
        timer.start();
    }
    

    }

    splash.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".FullscreenActivity" >
    
    <!--
         The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc.
    -->
    
    <TextView
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:keepScreenOn="true"
        android:text="@string/dummy_content"
        android:textColor="#33b5e5"
        android:textSize="50sp"
        android:textStyle="bold" />
    
    <!--
         This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows.
    -->
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true" >
    
        <LinearLayout
            android:id="@+id/fullscreen_content_controls"
            style="?buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:background="@color/black_overlay"
            android:orientation="horizontal"
            tools:ignore="UselessParent" >
    
        </LinearLayout>
    </FrameLayout>
    
    </FrameLayout>
    

    的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.first"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.first.FullscreenActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.first.Splash"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
    </application>
    
    </manifest>
    

1 个答案:

答案 0 :(得分:2)

在splash.class上,更改

Intent start = new Intent("com.example.first.FullscreenActivity");

Intent start = new Intent(Splash.this,com.example.first.FullscreenActivity.class);

您将manifest.xml更改为以下

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

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.first.Splash"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.first.FullscreenActivity"
        android:configChanges="orientation|keyboardHidden|screenSize">
    </activity>

</application>

</manifest>