Android Splash屏幕活动

 中医鸣芳 发布于 2023-01-19 11:00

我正在创建一个Android应用程序,当我在我的三星Galaxy上调试它时,Splash活动首先加载,因为它应该,但在此之后应用程序在执行"Splash"活动后立即崩溃/停止.线程休眠5秒后,它不会进入"MainActivity"活动.有谁知道可能导致问题的原因是什么?此外,在我尝试调试应用程序并将其加载到手机后,应用程序甚至没有显示出来.我顺便使用Eclipse.它在我的手机上显示应用程序管理器中的应用程序,但它不会在我的应用程序屏幕中显示该图标.

这是我的Splash.java:

package com.example.mihirsandroidapp;

import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle;

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent openMainActivity =  new Intent("com.example.mihirandroidsapp.MAINACTIVITY");
                startActivity(openMainActivity);
            }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}


}

这是我的清单:





       
        
            
            
        
    
        
        
            
            
        
    

这是我的主要活动,应该在启动画面后启动:

package com.example.mihirsandroidapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

int counter;
Button add, sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter += 1;
            display.setText("Total is " + counter);


        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter -= 1;
            display.setText("Total is " + counter);

        }
    });

}

}

Pavel Dudka.. 13

哦..我从哪里开始..让我们解决所有问题:

1)修复你的清单.绝对不是宣布你的活动的正确方法.这是它应该是什么样子:




    
        
            
            
        
    
    
    

2)现在让我们来解决你开始活动的方式:

Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);

3)不要叫finish()onPause()-你打破天然活性生命周期流程.finish()开始新活动后立即致电:

Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();

4)而不是创建单独的线程,只需创建一个Handler并在Runnable那里发布5秒延迟:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //this will be called after 5 seconds delay
    }
}, 5000);    

这是整个文件放在一起:

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
            startActivity(openMainActivity);
            finish();

        }
    }, 5000);    
}

如果它没有帮助 - 我们肯定需要看看logcat输出...

1 个回答
  • 哦..我从哪里开始..让我们解决所有问题:

    1)修复你的清单.绝对不是宣布你的活动的正确方法.这是它应该是什么样子:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    
    <application
        android:debuggable="true"
        android:allowBackup="true"
        android:icon="@drawable/cartooncat"
        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=".MainActivity"
            android:label="@string/app_name" >
        </activity>
    </application>
    

    2)现在让我们来解决你开始活动的方式:

    Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
    

    3)不要叫finish()onPause()-你打破天然活性生命周期流程.finish()开始新活动后立即致电:

    Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
    startActivity(openMainActivity);
    finish();
    

    4)而不是创建单独的线程,只需创建一个Handler并在Runnable那里发布5秒延迟:

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //this will be called after 5 seconds delay
        }
    }, 5000);    
    

    这是整个文件放在一起:

    public class Splash extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
                startActivity(openMainActivity);
                finish();
    
            }
        }, 5000);    
    }
    

    如果它没有帮助 - 我们肯定需要看看logcat输出...

    2023-01-19 11:03 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有