所以我有这个代码,但是当我在手机上尝试它时却没有做任何事情。
我也在firebase网站上试过这个版本,但它不起作用。如果你能帮助我们,我真的很感激!
我认为这是the problem 有人可以帮我吗?
public class LoginActivity extends AppCompatActivity {
public Firebase mFirebase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
mFirebase = new Firebase("https://kip.firebaseio.com");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
public void onClickLogin(View v){
final String email = ((EditText) findViewById(R.id.email)).getText().toString();
final String password =((EditText) findViewById(R.id.password)).getText().toString();
mFirebase.createUser(email, password, new Firebase.ResultHandler() {
@Override
public void onSuccess() {
mFirebase.authWithPassword(email, password, null);
System.out.println("Successfully created user account!");
}
@Override
public void onError(FirebaseError firebaseError) {
mFirebase.authWithPassword(email, password, null);
System.out.println("There was an error connecting!");
}
});
}
}
这是我的XML
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/button_login"
android:text="@string/login"
android:textColor="@android:color/white"
android:textSize="16sp"
android:id="@+id/button"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:onClick="onClickLogin"
android:clickable="true" />
答案 0 :(得分:1)
尝试:
public class LoginActivity extends AppCompatActivity {
public Firebase mFirebase;
Button button = (Button) findViewById(R.id.button);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
mFirebase = new Firebase("https://kip.firebaseio.com");
requestWindowFeature(Window.FEATURE_NO_TITLE);
button.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
final String email = ((EditText) findViewById(R.id.email)).getText().toString();
final String password =((EditText) findViewById(R.id.password)).getText().toString();
mFirebase.createUser(email, password, new Firebase.ResultHandler() {
@Override
public void onSuccess() {
mFirebase.authWithPassword(email, password, null);
System.out.println("Successfully created user account!");
}
@Override
public void onError(FirebaseError firebaseError) {
mFirebase.authWithPassword(email, password, null);
System.out.println("There was an error connecting!");
}
});
}
});
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
}
答案 1 :(得分:0)
这是最终的代码,它有效!!
protected EditText mEmail;
protected EditText mPassword;
protected Button mLogin;
protected Button mRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
mEmail = (EditText) findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mLogin = (Button) findViewById(R.id.Login);
mRegister = (Button) findViewById(R.id.Register);
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase ref = new Firebase("https://kip.firebaseio.com");
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
Intent activity = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(activity);
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
// there was an error
}
});
}
});
mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase ref = new Firebase("https://kip.firebaseio.com");
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
ref.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));
}
@Override
public void onError(FirebaseError firebaseError) {
// there was an error
}
});
}
});
}
}