我收到的消息是
需要java.lang.String
代码中突出显示的行是 Common.CurrentUser=login;
,它位于 MainActivity 的signIn
方法中。
我是初学者,请帮助我。提前致谢。我的意图有什么问题吗?我怎么调试这个? sholud我在android studio中使用“debug”选项?
package sonu.enigma;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
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;
import com.rengwuxian.materialedittext.MaterialEditText;
import sonu.enigma.Common.Common;
import sonu.enigma.Model.User;
import static sonu.enigma.R.drawable.l;
public class MainActivity extends AppCompatActivity {
MaterialEditText edtNewUser, edtNewPassword, edtNewEmail; //for Sign Up
MaterialEditText edtUser, edtPassword; //for Sign In
Button btnSignUp, btnSignIn;
FirebaseDatabase database;
DatabaseReference users;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Firebase
database=FirebaseDatabase.getInstance();
users=database.getReference("Users");
edtUser=(MaterialEditText)findViewById(R.id.edtUserName);
edtPassword=(MaterialEditText)findViewById(R.id.edtPassword);
btnSignIn=(Button)findViewById(R.id.btn_sign_in);
btnSignUp=(Button)findViewById(R.id.btn_sign_up);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSignUpDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn(edtUser.getText().toString(), edtPassword.getText().toString());
}
});
}
private void signIn(final String user, final String pwd) {
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(user).exists()){
if (!user.isEmpty()){
User login=dataSnapshot.child(user).getValue(User.class);
if (login.getPassword().equals(pwd)){
Intent homeActivity = new Intent(MainActivity.this, Home.class);
Common.CurrentUser=login;
startActivity(homeActivity);
finish();
}
else {
Toast.makeText(MainActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(MainActivity.this, "Please enter your User name", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(MainActivity.this, "User is not exists!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showSignUpDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Sign Up");
alertDialog.setMessage("Kindly, fill the information!");
LayoutInflater inflater= this.getLayoutInflater();
View sign_up_layout= inflater.inflate(R.layout.sign_up_layout, null);
edtNewUser=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewUserName);
edtNewEmail=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewEmail);
edtNewPassword=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewPassword);
alertDialog.setView(sign_up_layout);
alertDialog.setIcon(R.drawable.ic_account_circle_black_24dp);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
final User user= new User(edtNewUser.getText().toString(),
edtNewPassword.getText().toString(),
edtNewEmail.getText().toString());
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(user.getUserName()).exists())
Toast.makeText(MainActivity.this, "User already exists!", Toast.LENGTH_SHORT).show();
else {
users.child(user.getUserName())
.setValue(user);
Toast.makeText(MainActivity.this, "User Registration Successful!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
dialogInterface.dismiss();
}
});
alertDialog.show();
}
}
为什么显示此错误?我正在使用firebase作为后端制作在线测验应用程序。
package sonu.enigma.Model;
/**
* Created by PAWAN on 01-10-2017.
*/
public class User {
private String userName;
private String password;
private String email;
public User(){
}
public User(String userName, String password, String email){
this.userName=userName;
this.password=password;
this.email =email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
答案 0 :(得分:1)
login是用户数据model.its not string .so分配字符串值
使用代码
Common.CurrentUser=login.getUserName();
而不是
Common.CurrentUser=login
答案 1 :(得分:0)
显然,Common.CurrentUser
的类型为String
且login
的类型不是(可能是DataSnapshot
)。尝试使用Common.CurrentUser = login.getUserName();
或者将CurrentUser
类中的字段Common
的类型从字符串更改为DataSnapshot