2013-09-24 60 views
0

我對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("[email protected]") && 
         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();       
    } 
    }; 
} 

飛濺類:

 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. 
+0

你在Logcat中有什麼異常嗎? –

+0

添加了控制檯...請參閱編輯 – adityawho

+0

發佈Logcat,而不是控制檯。 –

回答

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

} 
+0

什麼是飛濺超時? – adityawho

+0

SPLASH_TIME_OUT = 3000.因此,3秒後飛濺活動啓動主要活動。即時間間隔 – Lokesh

+0

請投票答覆,然後只有它會對其他人有用 – Lokesh

0

我覺得你已經在意向濾波部 首先做錯刪除意圖過濾代碼,第二個活動,並檢查輸出和它

評論

修改此行

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

​​
+0

不工作.... – adityawho

0

你應該使用下面的構造

Intent it = new Intent(packageContext, cls);// 

傳遞的意圖聲明你的意圖一樣,您要使用此

Intent it = new Intent(splash.this, LOGINACTIVITY.class);// 

構造是這需要action作爲參數和

隱含的意圖變更

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

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

splash是另一個活動,它不是包的上下文... – adityawho

+0

@ stylojack_10包含splash activty的類名稱將成爲您的上下文..eg如果您的類名稱是MainActivity那麼您的packageContext是MainActivity.this –

+0

我的項目也有飛濺和它的工作 –

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(); 
+0

雅這不工作.... – adityawho

+0

但你已經放置了一些這樣的事情:Intent openLoginActivity = new Intent( 「com.sanginfo.loginsample.LOGINACTIVITY」); –

+0

你的代碼也不起作用... – adityawho

0

嘗試從您的LoginActivity清單中刪除以下代碼。

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

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

沒有影響...... – adityawho

+0

你得到了什麼錯誤?請重建您的項目,然後執行全新安裝。 –