Splash Screen成无限循环

时间:2015-01-24 07:32:12

标签: java android

我正在开发一个Android应用程序,而不是加载下一个活动的启动画面,继续发布吐司并进入无限循环!我在这里发布了所需的代码和logcat:

SplashScreen.java:

  package com.example.ambuj.supercabs;

  import android.content.Intent;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.os.Handler;
  import android.app.Activity;


  public class SplashScreen extends ActionBarActivity {

  private final int SPLASH_DISPLAY_TIMER = 5000;


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

    Thread loading = new Thread() {
        public void run() {
            try {
                sleep(2000);
                Intent main = new Intent(SplashScreen.this,loginActivity.class);
                startActivity(main);
                finish();


            }

            catch (Exception e) {
                e.printStackTrace();
            }

            finally {
                SplashScreen.this.finish();
            }
        }
    };

    loading.start();
}


   /**new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent mainIntent = new Intent(SplashScreen.this,loginActivity.class);
            SplashScreen.this.startActivity(mainIntent);
            SplashScreen.this.finish();
        }
    },SPLASH_DISPLAY_TIMER);   }*/


@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_splash_screen, 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);
}
}

这是loginActivity.java:

package com.example.ambuj.supercabs;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android .content.Context;


public class loginActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Context context = getApplicationContext();
    CharSequence text = "Please fill your login details!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    toast.setGravity(Gravity.BOTTOM,0,0);
    loginActivity.this.startActivity(intent);
    loginActivity.this.finish();
}


@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_login, 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);
}

}

这是我的logcat:

01-24 12:44:34.050    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-24 12:44:34.050    7892-7892/com.example.ambuj.supercabs I/PersonaManager﹕ getPersonaService() name persona_policy
01-24 12:44:34.160    7892-7892/com.example.ambuj.supercabs D/dalvikvm﹕ GC_FOR_ALLOC freed 114K, 10% free 16889K/18652K, paused 23ms, total 23ms
01-24 12:44:34.190    7892-7892/com.example.ambuj.supercabs I/dalvikvm-heap﹕ Grow heap (frag case) to 23.322MB for 5474752-byte allocation
01-24 12:44:34.205    7892-7902/com.example.ambuj.supercabs D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 8% free 22235K/24000K, paused 18ms, total 18ms
01-24 12:44:34.270    7892-7892/com.example.ambuj.supercabs I/﹕ PLATFORM VERSION : JB-MR-2
01-24 12:44:34.285    7892-7892/com.example.ambuj.supercabs D/mali_winsys﹕ new_window_surface returns 0x3000
01-24 12:44:34.320    7892-7892/com.example.ambuj.supercabs D/OpenGLRenderer﹕ Enabling debug mode 0
01-24 12:44:36.235    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-24 12:44:36.240    7892-7892/com.example.ambuj.supercabs I/PersonaManager﹕ getPersonaService() name persona_policy
01-24 12:44:36.335    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-24 12:44:36.380    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-24 12:44:36.405    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-24 12:44:36.435    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-24 12:44:36.460    7892-7892/com.example.ambuj.supercabs W/ApplicationPackageManager﹕ getCSCPackageItemText()

2 个答案:

答案 0 :(得分:0)

如何处理Splash是

private static int SPLASH_TIME_OUT = 2000;//declare it as class level

然后在你的onCreate

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() {
                // This method will be executed once the
                // timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen0.this, SplashScreen1.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);

答案 1 :(得分:0)

   package com.example.ambuj.supercabs;

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Handler;
import android.app.Activity;


public class SplashScreen extends Activity {




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

Thread loading = new Thread() {
    public void run() {
        try {
            sleep(2000);
            Intent main = new Intent(SplashScreen.this,loginActivity.class);
            startActivity(main);
            finish();


        }

        catch (Exception e) {
            e.printStackTrace();
        }

    }
};

loading.start();
}
}