当我呼叫第二个活动(主要)时,应用程序停止

时间:2015-09-02 12:47:16

标签: java android android-intent android-activity

我有两个Activities。我在互联网上看到了这段代码,但应用程序在Splash完成()之后关闭。

public class Splash extends ActionBarActivity {
    private final int DURATION = 3000;
    private Thread mSplashThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mSplashThread = new Thread() {

        @Override
        public void run() {
            synchronized (this) {
                try {
                    wait(DURATION);
                } catch (InterruptedException e) {
                } finally {

                    Intent intent = new Intent(getBaseContext(),
                            Main.class);
                    startActivity(intent);
                    finish();
                }
            }
        }

    };
    mSplashThread.start();
}

我该怎么办?我想显示第一个Activity 3秒钟,然后我想转到名为Activity的第二个Main。拜托,帮助我!

这是我的机器人清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ale.bookoflife" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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=".Main"
            android:label="@string/title_activity_main" >
        </activity>
    </application>

</manifest>

这是我的主要活动

package com.ale.bookoflife;

import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class Main extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Font path
        String fontPath = "LSANS.ttf";

        // text view label
        TextView OurText = (TextView) findViewById(R.id.textView);

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

        // Applying font
        OurText.setTypeface(tf);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

&#34;该应用已停止&#34;是错误,然后崩溃。

我生气了,我做错了什么?

4 个答案:

答案 0 :(得分:0)

使用以下代码:

addressDict

答案 1 :(得分:0)

更好的方法来做你想要的是使用Handler,比如:

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {  
// here you write the code or function
             Intent intent = new Intent(Splash.this,
                        Main.class);
                startActivity(intent);
                this.finish();
        }
    }, 3000); //duration to wait  

详细了解Android os handler

如果app manifest中的活动名称不同,只需从互联网上复制粘贴代码即可。

答案 2 :(得分:0)

new Handler().postDelayed(new Runnable() {
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */
            @Override
            public void run() {


                Intent i = null;

                    i = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                finish();

            }
        }, 3000);




please add this to your menifeast file

<activity
            android:name=".view.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".view.LoginActivity"
            android:screenOrientation="portrait"></activity>

now it will work..

答案 3 :(得分:0)

由于您没有向我们提供日志,因此无法进行调试。 好吧,我认为该应用程序正在崩溃,因为你正在杀死UI线程。 尝试删除splash活动中的finish语句。