I am getting this error find view by id(int) in appcompatActivity cannot be applied to () and it comes on line 65

huangapple 未分类评论70阅读模式
英文:

I am getting this error find view by id(int) in appcompatActivity cannot be applied to () and it comes on line 65

问题

I am getting this error: 在 appcompatActivity 中找不到适用于 () 的 find view by id(int),并且该错误出现在第 65 行。我不知道如何修复它。我正在从 Firebase Firestore 调用数据,并尝试在文本视图中打印该值。

package net.simplifiedlearning.firebaseauth;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseFirestore db = FirebaseFirestore.getInstance();

    final String MyPREFERENCES = "MyPrefs";

    Button AddChoreButton;
    TextView totalPoints;
    SharedPreferences sharedpreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        AddChoreButton = (Button) findViewById(R.id.AddChoreButton);
        totalPoints = (TextView) findViewById(R.id.totalPoints);

        findViewById(R.id.AddChoreButton).setOnClickListener(this);

        preparepointdata();
    }

    private void preparepointdata() {
        final String MyPREFERENCES = "MyPrefs";

        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        String parentEmail = sharedpreferences.getString("parentEmail", null);
        String username = sharedpreferences.getString("username", null);

        db.collection("profiles").document(username)
                .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) // if username is present
                    {
                         totalPoints.setText(document.getData().get("totalPoints").toString()); //textView 的 id
                    } else {
                        // 添加代码(未找到/未注册用户)
                    }
                } else {
                    // 处理错误
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.AddChoreButton:
                startActivity(new Intent(ProfileActivity.this, AddChoreActivity.class));
        }
    }
}

这行代码出错:findViewById().setText(document.getData().get("totalPoints").toString());//textView 的 id。如果可以的话,请提供解决方案。谢谢。

英文:

I am getting this error find view by id(int) in appcompatActivity cannot be applied to () and it comes on line 65. I don't know how to fix it. I am calling data from firebase firestore and i am trying to print that value in the text view.

package net.simplifiedlearning.firebaseauth;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseFirestore db = FirebaseFirestore.getInstance();

    final String MyPREFERENCES = &quot;MyPrefs&quot;;


    Button AddChoreButton;
    TextView totalPoints;
    SharedPreferences sharedpreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        AddChoreButton = (Button) findViewById(R.id.AddChoreButton);
        totalPoints = (TextView) findViewById(R.id.totalPoints);


        findViewById(R.id.AddChoreButton).setOnClickListener(this);

        preparepointdata();

    }

    private void preparepointdata() {
        final String MyPREFERENCES = &quot;MyPrefs&quot;;

        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        String parentEmail = sharedpreferences.getString(&quot;parentEmail&quot;, null);
        String username = sharedpreferences.getString(&quot;username&quot;, null);

        db.collection(&quot;profiles&quot;).document(username)
                .get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
            @Override
            public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) // if username is present
                    {
                         findViewById().setText(document.getData().get(&quot;totalPoints&quot;).toString());//id of textView
                    } else {

                        //add code (user not found/registered)
                    }
                        } else {
                        }
                    }
                });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.AddChoreButton:
                startActivity(new Intent(ProfileActivity.this, AddChoreActivity.class));
        }
    }
}

the line that has an error is this line findViewById().setText(document.getData().get(&quot;totalPoints&quot;).toString());//id of textView. Please offer solutions if you can. Thank you

答案1

得分: 0

You have to add an id to findViewById()

For example:

findViewById(R.id.total_points_tv).setText(document.getData().get("totalPoints").toString())
英文:

You have to add an id to findViewById()

For example :

findViewById(R.id.total_points_tv).setText(document.getData().get(&quot;totalPoints&quot;).toString())

huangapple
  • 本文由 发表于 2020年8月14日 19:44:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/63412132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定