我是android studio的新手。在我的项目中,我创建了一个登录页面,并在Google上设置了Firebase帐户,并上传了一些数据(用户名,密码)。但是我无法连接到Firebase。每次运行它时,应用程序都会在我的手机上闪回。任何人都可以帮助我解决这个问题吗?
package com.example.edward.eventmanagementsystem;
import android.app.ProgressDialog;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.edward.eventmanagementsystem.model.Organizer;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
//private Button button;
Button loginButton;
EditText editLogin, editPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
editLogin = findViewById(R.id.editLogin);
editPass = findViewById(R.id.editPass);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent MainMenu = new Intent(MainActivity.this, com.example.edward.eventmanagementsystem.MainMenu.class);
startActivity(MainMenu);
}
});
// Init Database
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("Organizer");
loginButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View view){
final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
mDialog.setMessage("Please waiting...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//check if user does't exists in database
if(dataSnapshot.child(editLogin.getText().toString()).exists()) {
//get organizer sign in information
mDialog.dismiss();
Organizer organizer = dataSnapshot.child(editLogin.getText().toString()).getValue(Organizer.class);
if(organizer.getPassword().equals(editPass.getText().toString())){
Toast.makeText(MainActivity.this, "Sign in successfully!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Sign in failed!", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "User not exists in database.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
/** Called when the user taps the Send button
public void onClick(View view){
Intent intent = new Intent(this, MainMenu.class);
startActivity(intent);
}*/
}
答案 0 :(得分:0)
有两个用于登录Button
的侦听器,这甚至也让我感到困惑:
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent MainMenu = new Intent(MainActivity.this, com.example.edward.eventmanagementsystem.MainMenu.class);
startActivity(MainMenu);
}
});
loginButton.setOnClickListener(new View.OnClickListener(){
当您点击Button
时,它将转到另一个名为Activity
的{{1}}类 ,但是您还有另一个侦听器 使用Firebase的东西。
解决方案:
使用一个带有Firebase代码的监听器,并且当Firebase登录成功后,转到另一个MainMenu
:
Activity