“欢迎”屏幕将打开并倒数,但无法加载第二个布局,因此我陷入了“欢迎”屏幕。我尝试删除欢迎屏幕,但无法将用户ID上传到Firebase
。我可以上传用户图片,但无法加载用户ID。
我在做错什么还是没有得到什么?
这是我的register.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@mipmap/background"
tools:context=".RegisterActivity"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RelativeLayout1"
android:visibility="visible"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:layout_centerInParent="true"
android:layout_centerVertical="false"
android:gravity="center"
android:text="Welcome "
android:textSize="30sp" />
</RelativeLayout>
<TextView
android:id="@+id/register_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
android:layout_above="@id/RelativeLayout1"
android:fontFamily="@font/nunito_semibold"
android:text="Create a new Accocunt"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="24sp" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/register_image_btn"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/register_heading"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="@android:color/transparent"
android:src="@mipmap/default_image"
android:layout_above="@id/RelativeLayout1"/>
<EditText
android:id="@+id/register_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/register_image_btn"
android:layout_centerHorizontal="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_background"
android:ems="10"
android:textColor="@color/colorPrimary"
android:fontFamily="@font/nunito_semibold"
android:hint="Name..."
android:inputType="textPersonName"
android:paddingBottom="12dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="12dp"
android:textSize="16sp"
android:layout_above="@id/RelativeLayout1"/>
<Button
android:id="@+id/register_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/register_name"
android:layout_centerHorizontal="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_background"
android:text="Next"
android:textAllCaps="false"
android:textColor="@color/colorPrimary"
android:layout_above="@id/RelativeLayout1"/>
<ProgressBar
android:id="@+id/registerProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:indeterminate="true"
android:visibility="invisible"
android:layout_above="@id/RelativeLayout1"/>
</RelativeLayout>
我的register.java
:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
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.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import de.hdodenhof.circleimageview.CircleImageView;
public class RegisterActivity extends AppCompatActivity {
private static final int PICK_IMAGE = 1;
private CircleImageView mImageBtn;
private EditText mEmailField;
private EditText mNameField;
private EditText mPasswordField;
private Button mRegBtn;
ArrayList<Photo> photoList;
private Uri imageUri;
private StorageReference mStorage;
private FirebaseAuth mAuth;
private FirebaseFirestore mFirestore;
DatabaseReference databaseReference;
private ProgressBar mRegisterProgressBar;
RelativeLayout RelativeLayout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
mFirestore = FirebaseFirestore.getInstance();
RelativeLayout1=(RelativeLayout) findViewById(R.id.RelativeLayout1);
RelativeLayout1.setVisibility(View.VISIBLE);
String userID = mAuth.getCurrentUser().getUid();
DocumentReference mDocRef = mFirestore.collection("Users").document(userID);
mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
Toast.makeText(RegisterActivity.this, "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
}
public void onFinish() {
RelativeLayout1.setVisibility(View.GONE);
Toast.makeText(RegisterActivity.this, "done" , Toast.LENGTH_LONG).show();
sendToMain();
}
}.start();
//
}else {
RelativeLayout1.setVisibility(View.GONE);
}
}
});
imageUri = null;
mStorage = FirebaseStorage.getInstance().getReference().child("images");
mImageBtn = (CircleImageView) findViewById(R.id.register_image_btn);
mNameField = (EditText) findViewById(R.id.register_name);
mRegBtn = (Button) findViewById(R.id.register_btn);
mRegisterProgressBar = (ProgressBar) findViewById(R.id.registerProgressBar);
mRegBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
test();
}
});
mImageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
}
private void sendToMain() {
Intent mainIntent = new Intent(RegisterActivity.this, Main2Activity.class);
startActivity(mainIntent);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE){
imageUri = data.getData();
mImageBtn.setImageURI(imageUri);
}
}
private void test(){
if(imageUri != null){
mRegisterProgressBar.setVisibility(View.VISIBLE);
final String name = mNameField.getText().toString();
final String user_id = mAuth.getCurrentUser().getUid();
StorageReference user_profile = mStorage.child(user_id + ".jpg");
user_profile.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> uploadTask) {
if(uploadTask.isSuccessful()){
final String download_url = uploadTask.getResult().getDownloadUrl().toString();
String token_id = FirebaseInstanceId.getInstance().getToken();
Map<String, Object> userMap = new HashMap<>();
userMap.put("name", name);
userMap.put("image", download_url);
userMap.put("token_id", token_id);
mFirestore.collection("Users").document(user_id).set(userMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
databaseReference = FirebaseDatabase.getInstance().getReference("user");
String id = databaseReference.push().getKey();
photoList=new ArrayList<>();
User u = new User(user_id, name, photoList);
databaseReference.child(user_id).setValue(u);
mRegisterProgressBar.setVisibility(View.INVISIBLE);
sendToMain();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(RegisterActivity.this, "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
mRegisterProgressBar.setVisibility(View.INVISIBLE);
}
});
} else {
Toast.makeText(RegisterActivity.this, "Error : " + uploadTask.getException().getMessage(), Toast.LENGTH_LONG).show();
mRegisterProgressBar.setVisibility(View.INVISIBLE);
}
}
});
} else {
Toast.makeText(RegisterActivity.this, "Error : unable ", Toast.LENGTH_SHORT).show();
mRegisterProgressBar.setVisibility(View.INVISIBLE);
}
}
}