Android的闪屏活动Android

2023-09-07 02:09:24 作者:硪清高゛自傲↗

我创建一个Android应用程序,当我去调试它在我的三星Galaxy第一飞溅的活动负载,因为它应该,但这样做的泼水节活动之后,该应用程序崩溃后/停止。它不进入MainActivity活动的线程休眠5秒后。有谁知道什么可能导致这个问题?再加上后,我尝试调试应用程序并加载它放到我的手机应用程序甚至没有显示出来。我使用Eclipse的方式。它显示了我的手机在我的应用程序管理器应用程序,但它并没有显示在我的应用程序屏幕上的图标。

下面是我的Splash.java:

 包com.example.mihirsandroidapp;进口android.app.Activity;进口android.content.Intent;进口android.os.Bundle;公共类飞溅延伸活动{@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.splash);    螺纹定时器=新的Thread(){        公共无效的run(){            尝试{                睡眠(5000);            }赶上(InterruptedException的E){                e.printStackTrace();            }最后{                意图openMainActivity =新意图(com.example.mihirandroidsapp.MAINACTIVITY);                startActivity(openMainActivity);            }        }    };    timer.start();}@覆盖保护无效的onPause(){    super.onPause();    完();}} 

下面是我的清单:

 <?XML版本=1.0编码=UTF-8&GT?; 
android 画面闪烁,android 活动之间的图像过渡时闪烁屏幕

 <采用-SDK    安卓的minSdkVersion =8    机器人:targetSdkVersion =19/><应用    机器人:可调试=真    机器人:allowBackup =真    机器人:图标=@绘制/ cartooncat    机器人:标签=@字符串/ APP_NAME    机器人:主题=@风格/ AppTheme>       <活动        机器人:名字=泼水节        机器人:标签=@字符串/ APP_NAME>        &所述;意图滤光器>            <作用机器人:名字=com.example.mihirsandroidapp.SPLASH/>            <类机器人:名字=android.intent.category.LAUNCHER/>        &所述; /意图滤光器>    < /活性GT;        <活动        机器人:名字=。MainActivity        机器人:标签=@字符串/ APP_NAME>        &所述;意图滤光器>            <作用机器人:名字=com.example.mihirsandroidapp.MAINACTIVITY/>            <类机器人:名字=android.intent.category.DEFAULT/>        &所述; /意图滤光器>    < /活性GT;< /用途> 

这里是闪屏之后,应该开始我的主要活动:

 包com.example.mihirsandroidapp;进口android.app.Activity;进口android.os.Bundle;进口android.view.View;进口android.widget.Button;进口android.widget.TextView;公共类MainActivity延伸活动{INT计数器;按钮添加,子;TextView中显示;@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    计数器= 0;    添加=(按钮)findViewById(R.id.bAdd);    分=(按钮)findViewById(R.id.bSub);    显示=(的TextView)findViewById(R.id.tvDisplay);    add.setOnClickListener(新View.OnClickListener(){        @覆盖        公共无效的onClick(视图v){            计数器+ = 1;            display.setText(全是+计数器);        }    });    sub.setOnClickListener(新View.OnClickListener(){        @覆盖        公共无效的onClick(视图v){            计数器 -  = 1;            display.setText(全是+计数器);        }    });}} 

解决方案

呵呵..我从哪里开始。让我们通过所有的问题:

1)修正你的清单。绝对不是正确的方式来声明你的活动。下面是它看起来应该是这样的:

 <采用-SDK    安卓的minSdkVersion =8    机器人:targetSdkVersion =19/><应用    机器人:可调试=真    机器人:allowBackup =真    机器人:图标=@绘制/ cartooncat    机器人:标签=@字符串/ APP_NAME    机器人:主题=@风格/ AppTheme>    <活动        机器人:名字=泼水节        机器人:标签=@字符串/ APP_NAME>        &所述;意图滤光器>            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>        &所述; /意图滤光器>    < /活性GT;    <活动        机器人:名字=。MainActivity        机器人:标签=@字符串/ APP_NAME>    < /活性GT;< /用途> 

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

 意图openMainActivity =新意图(Splash.this,MainActivity.class); 

3)不要叫完成的onPause()() - 你打破本地活动的生命周期流程。呼叫完成()后立即启动新的活动:

 意图openMainActivity =新意图(Splash.this,MainActivity.class);startActivity(openMainActivity);完(); 

4)而不是创建独立线程的,只是创建一个处理程序和后的Runnable 那里与延时5秒:

 处理程序处理程序=新的处理程序();handler.postDelayed(新的Runnable(){    @覆盖    公共无效的run(){        //这会后延时5秒被称为    }},5000); 

下面是整个文件放在一起:

 公共类飞溅延伸活动{@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.splash);    处理程序处理程序=新的处理程序();    handler.postDelayed(新的Runnable(){        @覆盖        公共无效的run(){            意图openMainActivity =新意图(Splash.this,MainActivity.class);            startActivity(openMainActivity);            完();        }    },5000);} 

如果没有帮助 - 我们一定要看看logcat的输出...

I am creating an android app and when I go to debug it on my samsung galaxy the Splash activity loads first,as it should, but after that the app crashes/stops right after doing the "Splash" activity. It doesn't go to the "MainActivity" activity after the thread sleeps for 5 seconds. Does anyone know what might be causing the problem? Plus after I tried debugging the app and loaded it onto my phone the app isn't even showing up. I am using Eclipse by the way. It shows the app in my application manager on my phone but it doesn't show the icon in my app screen.

Here is my 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();
}


}

Here is my Manifest:

<?xml version="1.0" encoding="utf-8"?>

<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="com.example.mihirsandroidapp.SPLASH" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.mihirsandroidapp.MAINACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

And here is my main activity which should start after the splash screen:

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);

        }
    });

}

}

解决方案

Oh.. Where do I start.. Let's go through all of the issues:

1) Fix your manifest. Definitely not the right way to declare your activities. Here is what it should look like:

<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) Now let's fix the way you start your activity:

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

3) Don't call finish() in onPause() - you break native activity lifecycle flow. Call finish() right after you start new activity:

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

4) Instead of creating separate thread, just a create a Handler and post Runnable there with 5 seconds delay:

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

Here is entire file put together:

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);    
}

If it doesn't help - we definitely need to look at logcat output...