我的android应用链接到一个新活动,以允许用户创建其个人资料并将他们使用的字符串保存到firebase数据库中。但是,活动已加载但为空白,如下所示。仅在我添加了将用户数据保存到Firebase的功能之后,才出现此问题。 blank activity
package com.example.eventfeed;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class CreateProfileActivity extends AppCompatActivity {
public static final String INTEREST_KEY = "interest";
public static final String NAME_KEY = "name";
private DocumentReference mDocRef = RegisterActivity.getmDocRef();
private Button createBtn;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
this.setContentView(R.layout.create_profile_layout);
// Toast.makeText(this, "Create Activity", Toast.LENGTH_SHORT).show();
EditText interestsView = (EditText) findViewById(R.id.interests);
EditText nameView = (EditText) findViewById(R.id.profile_name);
String interests = interestsView.getText().toString();
String name= nameView.getText().toString();
Map<String, Object> dataToSave = new HashMap<>();
mDocRef = FirebaseFirestore.getInstance().document("users/" + ActivityLoginEmail.getEmailStr() + "/profileInfo");
dataToSave.put(INTEREST_KEY, interests);
dataToSave.put(NAME_KEY, name);
mDocRef.set(dataToSave);
createBtn = (Button) findViewById(R.id.createProfileBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CreateProfileActivity.this, Profile.class);
startActivity(intent);
}
});
}
}
格式化页面的xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/create_profile_layout"
tools:context=".CreateProfileActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/main_page_toolbar"
layout="@layout/app_bar_layout">
</include>
<include layout="@layout/create_profile_content"></include>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
创建个人资料活动xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".CreateProfileActivity"
android:id="@+id/create_profile_content"
tools:showIn="@layout/create_profile_layout">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/vibe_concert" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="56dp"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginTop="100dp"
app:srcCompat="@drawable/vibe_image" />
<EditText
android:id="@+id/profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginLeft="158dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="156dp"
android:layout_marginRight="156dp"
android:layout_marginBottom="228dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:layout_constraintVertical_bias="0.553"
/>
<TextView
android:id="@+id/interest_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="224dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/profile_interest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
tools:text="Interests" />
<EditText
android:id="@+id/interests"
android:layout_width="200dp"
android:layout_height="87dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="140dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4"
app:layout_constraintVertical_bias="0.091" />
<TextView
android:id="@+id/profile_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.679"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.544" />
<Button
android:id="@+id/createProfileBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.835"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/interests"
app:layout_constraintVertical_bias="0.473"
tools:text="Create Profile" />
</android.support.constraint.ConstraintLayout>
将用户发送到创建个人资料活动的调用方法
private void LoginUser(){
setEmailStr(email.getText().toString().trim());
String Password = password.getText().toString().trim();
mAuth.signInWithEmailAndPassword(emailStr, Password)
.addOnCompleteListener(this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
finish();
Toast.makeText(ActivityLoginEmail.this, "Create
Activity", Toast.LENGTH_SHORT).show();
setCurrent_user_db(false);
startActivity(new Intent(ActivityLoginEmail.this,
CreateProfileActivity.class));
}else {
setCurrent_user_db(true);
Toast.makeText(ActivityLoginEmail.this, "couldn't login",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
编辑:将xml文件更改为只有一个按钮的简单文件,并添加了toast命令以在活动未加载时进行打印。因此,问题很可能不在xml文件中。
EDIT2:我通过从oncreate方法中删除PersistableBundle参数找到了解决方案。如果有人想详细说明,则不确定该怎么做。
public class CreateProfileActivity extends AppCompatActivity {
public static final String INTEREST_KEY = "interest";
public static final String NAME_KEY = "name";
private DocumentReference mDocRef = RegisterActivity.getmDocRef();
private Button createBtn;
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.create_profile_layout);
Toast.makeText(this, "Create Activity", Toast.LENGTH_SHORT).show();
EditText interestsView = (EditText) findViewById(R.id.interests);
EditText nameView = (EditText) findViewById(R.id.profile_name);
String interests = interestsView.getText().toString();
String name= nameView.getText().toString();
Map<String, Object> dataToSave = new HashMap<>();
mDocRef = FirebaseFirestore.getInstance().document("users/" +
ActivityLoginEmail.getEmailStr() + "/profileInfo");
dataToSave.put(INTEREST_KEY, interests);
dataToSave.put(NAME_KEY, name);
mDocRef.set(dataToSave);
createBtn = (Button) findViewById(R.id.createProfileBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CreateProfileActivity.this, Profile.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:0)
通过将finish()
移至此条件的底部来尝试
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
Toast.makeText(ActivityLoginEmail.this, "Create
Activity", Toast.LENGTH_SHORT).show();
setCurrent_user_db(false);
startActivity(new Intent(ActivityLoginEmail.this,
CreateProfileActivity.class));
finish();
}