我是android studio的新手。我想在用户注册我的应用程序后将用户信息保存到数据库中。用户可以正确注册。但是,当我想添加将用户保存到数据库的代码时,会引起问题。当我想将其运行到我的设备时,该应用会强制关闭。但是,如果我删除与将用户保存到数据库相关的代码,它可以再次运行。有人可以帮我吗?
注册活动
package com.example.ilham.dompettebal;
import android.content.Intent;
import android.service.autofill.UserData;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
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;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.w3c.dom.Text;
import java.util.HashMap;
public class register extends AppCompatActivity {
private EditText Inputemail,Inputusername,Inputpassword;
private TextView signin;
private Button buttonRegister;
private FirebaseAuth auth;
private DatabaseReference users;
String userId;
// private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
auth = FirebaseAuth.getInstance();
userId = auth.getCurrentUser().getUid();
users = FirebaseDatabase.getInstance().getReference().child("users").child(userId);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
Inputemail = (EditText) findViewById(R.id.email);
Inputpassword = (EditText) findViewById(R.id.password);
Inputusername = (EditText) findViewById(R.id.username);
signin = (TextView) findViewById(R.id.signin) ;
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(register.this, login.class));
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = Inputemail.getText().toString().trim();
String password = Inputpassword.getText().toString().trim();
String username = Inputusername.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
// progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(register.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(register.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
// progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(register.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
SavedUser();
startActivity(new Intent(register.this, MainActivity.class));
finish();
}
}
});
}
});
}
private void SavedUser()
{
Inputemail = (EditText) findViewById(R.id.email);
Inputpassword = (EditText) findViewById(R.id.password);
Inputusername = (EditText) findViewById(R.id.username);
String email = Inputemail.getText().toString().trim();
String password = Inputpassword.getText().toString().trim();
String username = Inputusername.getText().toString().trim();
HashMap userData = new HashMap();
userData.put("username",username);
userData.put("email",email);
users.updateChildren(userData).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful())
{
Toast.makeText(register.this, "User Created" + task.getException(),
Toast.LENGTH_SHORT).show();
}
}
});
}
}
应用程序构建gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.ilham.dompettebal"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
}
logcat
--------- beginning of crash
2018-12-08 17:32:04.680 5405-28014/? V/FA-SVC: Scheduling upload with DelayedRunnable
2018-12-08 17:32:04.680 5405-28014/? V/FA-SVC: Scheduling upload with GcmTaskService
2018-12-08 17:32:04.680 5405-28014/? V/FA-SVC: Scheduling task with Gcm. time: 6553
2018-12-08 17:32:04.689 5405-28014/? V/FA-SVC: Background event processing time, ms: 20
2018-12-08 17:32:06.138 28480-28605/? V/FA: Inactivity, disconnecting from the service
2018-12-08 17:32:11.239 5405-28014/? V/FA-SVC: Cancelling job. JobID: 812057698
2018-12-08 17:32:11.251 5405-28014/? V/FA-SVC: Starting upload from DelayedRunnable
2018-12-08 17:32:11.266 5405-28014/? D/FA-SVC: Uploading events. Elapsed time since last upload attempt (ms): 10023
2018-12-08 17:32:11.271 5405-28014/? V/FA-SVC: Fetching remote configuration: com.example.ilham.dompettebal
2018-12-08 17:32:11.271 5405-28014/? V/FA-SVC: Not stopping services. fetch, network, upload: true, false, false
2018-12-08 17:32:11.340 5405-28014/? V/FA-SVC: onConfigFetched. Response size: 0
2018-12-08 17:32:11.345 5405-28014/? V/FA-SVC: Successfully fetched config. Got network response. code, size: 304, 0
2018-12-08 17:32:11.357 5405-28014/? D/FA-SVC: Updated lifetime engagement user property with value. Value: 3726
2018-12-08 17:32:11.370 5405-28014/? V/FA-SVC: Saving bundle, size: 472
2018-12-08 17:32:11.372 5405-28014/? D/FA-SVC: Uploading events. Elapsed time since last upload attempt (ms): 10120
2018-12-08 17:32:11.376 5405-28014/? V/FA-SVC: Uploading data. app, uncompressed size, data: com.example.ilham.dompettebal, 601,
batch {
bundle {
protocol_version: 1
platform: android
gmp_version: 12780
uploading_gmp_version: 14799
config_version: 1544238577803732
gmp_app_id: 1:239987278985:android:fb89b133b1f5d35b
app_id: com.example.ilham.dompettebal
app_version: 1.0
app_version_major: 1
firebase_instance_id: eTilxcjKV-M
dev_cert_hash: 2862194538385679336
app_store: manual_install
upload_timestamp_millis: 1544265131349
start_timestamp_millis: 1544265120649
end_timestamp_millis: 1544265124629
app_instance_id: 9dc9a14314926e5d2fac2ad1bc2f0292
resettable_device_id: 0dccc0aa-8c2d-43ee-b733-bcc037de7f18
limited_ad_tracking: false
os_version: 7.0
device_model: Redmi Note 4
user_default_language: en-us
time_zone_offset_minutes: 420
bundle_sequential_index: 1
service_upload: true
user_property {
set_timestamp_millis: 1544265120649
name: first_open_time(_fot)
int_value: 1544266800000
}
user_property {
set_timestamp_millis: 1544265120649
name: first_open_after_install(_fi)
int_value: 1
}
user_property {
set_timestamp_millis: 1544265131354
name: lifetime_user_engagement(_lte)
int_value: 3726
}
event {
name: first_open(_f)
timestamp_millis: 1544265120649
previous_timestamp_millis: 0
param {
name: firebase_conversion(_c)
int_value: 1
}
param {
name: firebase_event_origin(_o)
string_value: auto
}
param {
name: _r
int_value: 1
}
param {
name: previous_first_open_count(_pfo)
int_value: 11
}
param {
name: system_app(_sys)
int_value: 0
}
param {
name: update_with_analytics(_uwa)
int_value: 0
}
param {
name: system_app_update(_sysu)
int_value: 0
}
}
event {
name: user_engagement(_e)
timestamp_millis: 1544265120649
previous_timestamp_millis: 0
param {
name: firebase_event_origin(_o)
string_value: auto
}
param {
name: engagement_time_msec(_et)
int_value: 1
}
}
event {
name: screen_view(_vs)
timestamp_millis: 1544265120984
previous_timestamp_millis: 0
param {
name: firebase_event_origin(_o)
string_value: auto
}
param {
name: firebase_screen_class(_sc)
string_value: login
}
param {
name: firebase_screen_id(_si)
int_value: -1815647252065091777
}
}
event {
name: user_engagement(_e)
timestamp_millis: 1544265124629
previous_timestamp_millis: 1544265120649
param {
name: firebase_event_origin(_o)
string_value: auto
}
param {
name: engagement_time_msec(_et)
int_value: 3725
}
param {
name: firebase_screen_class(_sc)
string_value: login
}
param {
name: firebase_screen_id(_si)
int_value: -1815647252065091777
}
}
}
}
2018-12-08 17:32:11.376 5405-28014/? V/FA-SVC: Not stopping services. fetch, network, upload: true, true, false
2018-12-08 17:32:11.380 5405-28112/? V/FA-SVC: Uploading data. size: 484
2018-12-08 17:32:11.389 5405-28014/? V/FA-SVC: Not stopping services. fetch, network, upload: false, true, false
2018-12-08 17:32:11.438 5405-28014/? V/FA-SVC: Upload scheduled in approximately ms: 3599989
2018-12-08 17:32:11.441 5405-28014/? V/FA-SVC: Cancelling job. JobID: 812057698
2018-12-08 17:32:11.449 5405-28014/? V/FA-SVC: Scheduling upload with GcmTaskService
2018-12-08 17:32:11.449 5405-28014/? V/FA-SVC: Scheduling task with Gcm. time: 3599989
2018-12-08 17:32:11.456 5405-28014/? V/FA-SVC: Successful upload. Got network response. code, size: 204, 0
2018-12-08 17:32:11.474 5405-28014/? V/FA-SVC: Nothing to upload or uploading impossible
2018-12-08 17:32:11.476 5405-28014/? V/FA-SVC: Cancelling job. JobID: 812057698
2018-12-08 17:32:11.481 5405-28014/? V/FA-SVC: Stopping uploading service(s)
2018-12-08 17:32:17.009 28777-28777/? V/FA: Registered activity lifecycle callback
2018-12-08 17:32:17.302 28777-28799/? V/FA: Collection enabled
2018-12-08 17:32:17.303 28777-28799/? V/FA: App package, google app id: com.shopee.id, 1:808332928752:android:71c351ab322a4a71
2018-12-08 17:32:17.303 28777-28799/? I/FA: App measurement is starting up, version: 12780
2018-12-08 17:32:17.303 28777-28799/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2018-12-08 17:32:17.304 28777-28799/? I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.shopee.id
2018-12-08 17:32:17.304 28777-28799/? D/FA: Debug-level message logging enabled
2018-12-08 17:32:17.317 28777-28799/? V/FA: Connecting to remote service
2018-12-08 17:32:17.338 28777-28799/? V/FA: Connection attempt already in progress
2018-12-08 17:32:17.339 28777-28799/? D/FA: Connected to remote service
2018-12-08 17:32:17.340 28777-28799/? V/FA: Processing queued up service tasks: 2
2018-12-08 17:32:22.367 28777-28799/? V/FA: Inactivity, disconnecting from the service
2018-12-08 17:32:47.169 28859-28859/? I/FA: App measurement is starting up, version: 11200
2018-12-08 17:32:47.169 28859-28859/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2018-12-08 17:32:47.180 28859-28859/? V/FA: Collection enabled
2018-12-08 17:32:47.180 28859-28859/? V/FA: App package, google app id: com.google.android.apps.fireball, 1:81793200174:android:a3cee0fee63b6b7f
2018-12-08 17:32:47.181 28859-28859/? I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.google.android.apps.fireball
2018-12-08 17:32:47.181 28859-28859/? D/FA: Debug-level message logging enabled
2018-12-08 17:32:47.186 28859-28859/? V/FA: Cancelling job. JobID: -1358375162
2018-12-08 17:32:47.187 28859-28859/? V/FA: Registered activity lifecycle callback
2018-12-08 17:32:47.194 28859-28882/? V/FA: Using measurement service
2018-12-08 17:32:47.195 28859-28882/? V/FA: Connecting to remote service
2018-12-08 17:32:47.205 28859-28882/? V/FA: Using measurement service
2018-12-08 17:32:47.206 28859-28882/? V/FA: Connection attempt already in progress
2018-12-08 17:32:47.370 28859-28882/? D/FA: Connected to remote service
2018-12-08 17:32:47.370 28859-28882/? V/FA: Processing queued up service tasks: 2
2018-12-08 17:32:52.402 28859-28882/? V/FA: Inactivity, disconnecting from the service