[英]Android Studio: 2 XML Files, but App is starting 2nd XML-file instead of 1st one
我制作了一個允許用戶登錄的應用程序,但是每次我嘗試運行我的應用程序時,它都會自動顯示Activity_main.xml(如果成功登錄,該頁面即會顯示)。
我想運行我的應用程序, 但它必須首先顯示activity_main.xml(這是登錄頁面),而不是現在顯示第一頁的activity_main.xml。
這是我的代碼:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mertino11.ourapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".AccountActivity"
android:theme="@style/AppTheme" />
</application>
</manifest>
activity_account.xml(用戶成功登錄的XML)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_account"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mertino11.ourapplication.AccountActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Account Page"
android:ems="10"
android:id="@+id/editText"
android:textSize="22sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textStyle="normal|bold"
android:textAlignment="center" />
</RelativeLayout>
activity_main.xml(登錄頁面)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:baselineAligned="false">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/emailField"
android:hint="Email"
android:paddingTop="20dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/passwordField"
android:hint="Password"
android:fontFamily="sans-serif"
android:paddingTop="20dp" />
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginBtn"
android:paddingTop="20dp" />
</LinearLayout>
Java->類:AccountActivity(用戶成功登錄時的頁面)
package com.example.mertino11.ourapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class AccountActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
}
}
Java->類:FireApp(我認為Firebase設置)
package com.example.mertino11.ourapplication;
import android.app.Application;
import com.firebase.client.Firebase;
/**
* Created by Mertino11 on 10-Dec-16.
*/
public class FireApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Java->類:主要活動(使用帳戶進行后端日志記錄)
package com.example.mertino11.ourapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private EditText mEmailField;
private EditText mPasswordField;
private Button mLoginBtn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mEmailField = (EditText) findViewById(R.id.emailField);
mPasswordField = (EditText) findViewById(R.id.passwordField);
mLoginBtn = (Button) findViewById(R.id.loginBtn);
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, AccountActivity.class));
}
}
};
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startSignIn();
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void startSignIn() {
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "Fields are empty!", Toast.LENGTH_LONG).show();
} else {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(MainActivity.this, "Sign In Problem!", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
登錄運行:
12/10 16:35:26: Launching app
$ adb push C:\Users\Mertino11\Downloads\OurApplication\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.mertino11.ourapplication
$ adb shell pm install -r "/data/local/tmp/com.example.mertino11.ourapplication"
Success
$ adb shell am start -n "com.example.mertino11.ourapplication/com.example.mertino11.ourapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 2403 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.example.mertino11.ourapplication-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.mertino11.ourapplication, real application class is com.example.mertino11.ourapplication.FireApp.
[ 12-10 16:35:30.033 1550: 1571 D/ ]
HostConnection::get() New Host Connection established 0x89bee200, tid 1571
W/System: ClassLoader referenced unknown path: /data/app/com.example.mertino11.ourapplication-1/lib/x86
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
W/System: ClassLoader referenced unknown path:
W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
I/FA: App measurement is starting up, version: 10084
I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
D/FA: Debug-level message logging enabled
D/FA: AppMeasurement singleton hash: 174613062
I/art: Background sticky concurrent mark sweep GC freed 21982(2MB) AllocSpace objects, 55(1056KB) LOS objects, 34% free, 7MB/11MB, paused 7.752ms total 131.168ms
V/FA: Collection enabled
V/FA: App package, google app id: com.example.mertino11.ourapplication, 1:113712880364:android:c60a6cfc3967db90
I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.mertino11.ourapplication
V/FA: Registered activity lifecycle callback
I/FirebaseInitProvider: FirebaseApp initialization successful
V/FA: Using measurement service
V/FA: Connecting to remote service
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
V/FA: onActivityCreated
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 29161
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 2587: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa71b42c0, error=EGL_BAD_MATCH
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 497
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Activity paused, time: 29657
I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 3
V/FA: onActivityCreated
V/FA: Activity resumed, time: 30137
E/EGL_emulation: tid 2587: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa71b42c0, error=EGL_BAD_MATCH
V/FA: Inactivity, disconnecting from the service
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
V/FA: Session started, time: 39646
I/FA: Tag Manager is not found and thus will not be used
D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=AccountActivity, _si=-1992202464745463440}]
V/FA: Using measurement service
V/FA: Connecting to remote service
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
V/FA: Inactivity, disconnecting from the service
我對項目本身的層次結構的屏幕截圖:
http://i66.tinypic.com/vor192.png
該應用程序的場景:
注意:我是AndroidStudio的業余/初學者。
我沒有正確地提出問題。無論如何,在您的manifest.xml中,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mertino11.ourapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".AccountActivity"
android:theme="@style/AppTheme" />
</application>
</manifest>
MainActivity
內部的intent-filter表示MainActivity
是啟動程序Activity
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
因此,如果您想先啟動AccountActivity
改為
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mertino11.ourapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme"/>
<activity
android:name=".AccountActivity"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
1)您已經使用電子郵件和密碼登錄,這導致應用程序進入帳戶活動
2)可以防止使用
FirebaseAuth.getInstance().signOut();
3)更詳細的FireBase底部
4)謝謝你
Button logout=(Button) findViewById(R.id.logout)
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(AccountActivity.this,MainActivity.class));
finish();
}
});
XML文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="logout"
android:id="@+id/logout"/>
在AccountActivity.xml中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.