我正在YouTube视频的帮助下构建一个android项目,当我尝试构建它时,它会显示以下错误消息。
错误:类User中的构造函数User无法应用于给定类型;
必需:String,String,String,String,Uri
找到:字符串,字符串
原因:实际和正式论点清单的长度不同
我在视频上做了完全一样的事情,但现在我不明白为什么它会给我这样的错误。这是有错误的行。星号之间的线在代码中用红色下划线。
Common.loggedUser = **new User(firebaseUser.getUid(), firebaseUser.getEmail())**;
user_information.child(Common.loggedUser.**getUid**())
.setValue(Common.loggedUser);
Paper.book().write(Common.USER_UID_SAVE_KEY, Common.loggedUser.**getUid**());
tokens.child(firebaseUser.getUid())
.setValue(InstanceIdResult.**getToken**());
这是我的完整代码
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.User;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import com.mynewrealtimelocationtracker.Utils.Common;
import java.util.Arrays;
import java.util.List;
import io.paperdb.Paper;
public class MainActivity extends AppCompatActivity {
DatabaseReference user_information;
private static final int MY_REQUEST_CODE = 4477; //any number you want
List<AuthUI.IdpConfig> providers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Paper.init(this);
//Init firebase
user_information = FirebaseDatabase.getInstance().getReference(Common.USER_INFORMATION);
//Init provider
providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()
);
//Request permission location
Dexter.withActivity(this)
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
showSignInOptions();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(MainActivity.this,"You must accept permissionto use app",Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
}
}).check();
}
private void showSignInOptions() {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),MY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_REQUEST_CODE){
IdpResponse response = IdpResponse.fromResultIntent(data);
if (resultCode == RESULT_OK){
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
//Check if user exist on database
user_information.orderByKey()
.equalTo(firebaseUser.getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null) //If user is not exist
{
if (!dataSnapshot.child(firebaseUser.getUid()).exists()) //If key uid is not exist
{
Common.loggedUser = new User(firebaseUser.getUid(), firebaseUser.getEmail()); //--> Error
//Add to database
user_information.child(Common.loggedUser.getUid()) //->Error
.setValue(Common.loggedUser);
}
}
else // If user is available
{
Common.loggedUser = dataSnapshot.child(firebaseUser.getUid()).getValue(User.class);
}
// Save UID to storage to update location from background
Paper.book().write(Common.USER_UID_SAVE_KEY, Common.loggedUser.getUid()); //->Error
updateToken(firebaseUser);
setupUI();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
}
private void setupUI() {
//Navigate Home
startActivity(new Intent(MainActivity.this, HomeActivity.class));
}
private void updateToken(final FirebaseUser firebaseUser) {
final DatabaseReference tokens = FirebaseDatabase.getInstance()
.getReference(Common.TOKENS);
//Get Token
FirebaseInstanceId.getInstance().getInstanceId()
.addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
tokens.child(firebaseUser.getUid())
.setValue(InstanceIdResult.getToken()); //->Error
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的用户课程
package com.mynewrealtimelocationtracker.Model;
import java.util.HashMap;
public class User {
private String uid, email;
private HashMap<String, User> acceptList; //List user friend
public User() {
}
public User(String uid, String email) {
this.uid = uid;
this.email = email;
acceptList = new HashMap<>();
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public HashMap<String, User> getAcceptList() {
return acceptList;
}
public void setAcceptList(HashMap<String, User> acceptList) {
this.acceptList = acceptList;
}
}
红线无非是代码访问不到,或者语法错误,检查下
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。