模拟器不切换到下一个活动

时间:2013-09-24 09:29:55

标签: android splash-screen

我对android编程完全陌生。 我正在做一个简单的应用程序,我在其中显示启动画面,然后显示登录屏幕。问题是模拟器没有超出启动画面。 AndroidManifest:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/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=".LoginActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.loginsample.LOGINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

 </manifest>

LoginActivity:

package com.sanginfo.loginsample;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Patterns;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

 public class LoginActivity extends Activity {

CharSequence userName;
String  passWord;
Boolean rememberme;
EditText username, password;
CheckBox tempchkrememberme;
Button login;
private SharedPreferences objSP;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   /* if(objSP.getString("username", null) != null) {*/

    setContentView(R.layout.activity_login);
    // UI elements gets bind in form of Java Objects
    username = (EditText)findViewById(R.id.username);
    password = (EditText)findViewById(R.id.password);
    login = (Button)findViewById(R.id.login);
    tempchkrememberme = (CheckBox)findViewById(R.id.chkrememberme);

    objSP = this.getSharedPreferences("SharedPreferences", MODE_PRIVATE);
    SharedPreferences.Editor objEditor = objSP.edit();
    rememberme = objSP.getBoolean("rememberme", false);

    if (rememberme == true){
    username.setText(objSP.getString("username", ""));
    tempchkrememberme.setChecked(true);}
    //objEditor.putString("password", password.getText().toString());
    objEditor.commit();

    // now we have got the handle over the UI widgets
    // setting listener on Login Button
    // i.e. OnClick Event
    login.setOnClickListener(loginListener);  
}
//}

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

 private OnClickListener loginListener = new OnClickListener() {
    public void onClick(View v) {
        SharedPreferences.Editor objEditor = objSP.edit();
//getting inputs from user and performing data operations
        userName=username.getText().toString();
        String strUsername = username.getText().toString();
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
        login = (Button)findViewById(R.id.login);

        tempchkrememberme = (CheckBox)findViewById(R.id.chkrememberme);

        if (userName.length()==0) {
            username.requestFocus();
            username.setError("Email is required.");
            return;
            }

            boolean  isEmailValid = Patterns.EMAIL_ADDRESS.matcher(userName).matches();
            if (isEmailValid==false){
            username.requestFocus();
            username.setError("Email invalid.");
            return;
            }


        if(password.getText().toString().length() == 0 ){
            password.requestFocus();
            password.setError( "Password is required." );
            return; 
        }

        if (tempchkrememberme.isChecked()) {
            objEditor.putBoolean("rememberme", true);
            objEditor.putString("username", strUsername);
            //objEditor.putString("password", password);
            objEditor.commit();


        } else {
            objEditor.clear();
            objEditor.commit();

        }



          if(username.getText().toString().equals("aditya@sanginfo.com") &&
                      password.getText().toString().equals("password")){
// responding to the User inputs
                Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show(); 

          }else
                Toast.makeText(getApplicationContext(), "Invalid Credentials!",     Toast.LENGTH_LONG).show();                           
    }
  };
}

Splash Class:

        package com.sanginfo.loginsample;

  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(2000);
                }
                catch (InterruptedException ex){
                    ex.printStackTrace();
                }
                finally{
                    Intent openLoginActivity = new Intent(Splash.this, LoginActivity.class);
                    startActivity(openLoginActivity);
                }
            } 
         };
         timer.start();
    }
}

编辑: 控制台:

 [2013-09-24 15:02:58 - LoginSample] Android Launch!
 [2013-09-24 15:02:58 - LoginSample] adb is running normally.
 [2013-09-24 15:02:58 - LoginSample] Performing com.sanginfo.loginsample.Splash activity launch
 [2013-09-24 15:02:58 - LoginSample] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Nexus4'
 [2013-09-24 15:03:00 - LoginSample] Application already deployed. No need to reinstall.
 [2013-09-24 15:03:00 - LoginSample] Starting activity com.sanginfo.loginsample.Splash on device emulator-5554
 [2013-09-24 15:03:01 - LoginSample] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.sanginfo.loginsample/.Splash }
[2013-09-24 15:03:01 - LoginSample] ActivityManager: Warning: Activity not started, its current task has been brought to the front

红色的最后两个。

LogCat :(所有Es为红色)

 .....
 09-24 14:16:37.892: I/System.out(904): Sending WAIT chunk
 09-24 14:16:38.122: I/dalvikvm(904): Debugger is active
09-24 14:16:38.142: I/System.out(904): Debugger has connected
09-24 14:16:38.152: I/System.out(904): waiting for debugger to settle...
09-24 14:16:38.352: I/System.out(904): waiting for debugger to settle...
09-24 14:16:38.552: I/System.out(904): waiting for debugger to settle...
09-24 14:16:38.752: I/System.out(904): waiting for debugger to settle...
09-24 14:16:38.962: I/System.out(904): waiting for debugger to settle...
09-24 14:16:39.162: I/System.out(904): waiting for debugger to settle...
09-24 14:16:39.362: I/System.out(904): waiting for debugger to settle...
09-24 14:16:39.562: I/System.out(904): waiting for debugger to settle...
09-24 14:16:39.762: I/System.out(904): waiting for debugger to settle...
09-24 14:16:39.963: I/System.out(904): waiting for debugger to settle...
09-24 14:16:40.172: I/System.out(904): waiting for debugger to settle...
09-24 14:16:40.373: I/System.out(904): debugger has settled (1416)
09-24 14:16:42.502: D/gralloc_goldfish(904): Emulator without GPU emulation detected.
09-24 14:19:11.183: E/Trace(976): error opening trace file: No such file or directory (2)
09-24 14:19:12.423: D/gralloc_goldfish(976): Emulator without GPU emulation detected.
09-24 14:24:00.873: E/Trace(1024): error opening trace file: No such file or directory (2)
09-24 14:24:02.053: D/gralloc_goldfish(1024): Emulator without GPU emulation detected.
09-24 14:30:40.193: E/Trace(1070): error opening trace file: No such file or directory (2)
09-24 14:30:41.653: D/gralloc_goldfish(1070): Emulator without GPU emulation detected.
09-24 14:35:43.943: E/Trace(1117): error opening trace file: No such file or directory (2)
09-24 14:35:45.383: D/gralloc_goldfish(1117): Emulator without GPU emulation detected.
09-24 14:38:58.883: E/Trace(1191): error opening trace file: No such file or directory (2)
09-24 14:39:00.453: D/gralloc_goldfish(1191): Emulator without GPU emulation detected.
09-24 14:40:25.943: E/Trace(1237): error opening trace file: No such file or directory (2)
09-24 14:40:27.145: D/gralloc_goldfish(1237): Emulator without GPU emulation detected.
09-24 14:43:52.323: E/Trace(1309): error opening trace file: No such file or directory (2)
09-24 14:43:53.143: D/gralloc_goldfish(1309): Emulator without GPU emulation detected.
09-24 14:54:02.202: E/Trace(1358): error opening trace file: No such file or directory (2)
09-24 14:54:03.993: D/gralloc_goldfish(1358): Emulator without GPU emulation detected.

5 个答案:

答案 0 :(得分:1)

像这样使用:

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

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

        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(SplashScreen.this, MainActivity.class);
                startActivity(i);

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

}

答案 1 :(得分:0)

我认为你在intent-filter部分做错了   首先删除第二个活动中的意图过滤器代码并检查输出并对其进行评论

修改此行

 Intent openLoginActivity = new  Intent("com.sanginfo.loginsample.LOGINACTIVITY");

as

 Intent openLoginActivity = new  Intent("Splash.this","LOGINACTIVITY.class");

答案 2 :(得分:0)

你应该使用下面的构造函数

传递意图
  Intent it = new Intent(packageContext, cls);//

声明你的意图

 Intent it = new Intent(splash.this, LOGINACTIVITY.class);//
您正在使用的

构造函数用于隐式intent,它将action作为参数和

更改

  <action android:name="com.sanginfo.loginsample.LOGINACTIVITY" /> 

   <action android:name="com.sanginfo.loginsample.LoginActivity" />

答案 3 :(得分:0)

请查看意图,您必须在哪里设置您想要去的活动名称。

Thread th = new Thread(){
    public void run()
    {
        try {
            sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            Intent in = new Intent(getApplicationContext(),your class name here.class);
            startActivity(in);
        }
    }
};th.start();

答案 4 :(得分:0)

尝试从LoginActivity清单中删除以下代码。

    <intent-filter>
        <action android:name="com.sanginfo.loginsample.LOGINACTIVITY" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>