有人试图解决这个错误并帮助我,检查ss,我尝试了三种方法来解决,但没有用

qij5mzcb  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(288)


在firebaseadapter中,我在viewholderclass中为intent创建了视图

@Override
        protected void onBindViewHolder(@NonNull MyviewHolder holder, int position, @NonNull DataClass model) {
            holder.name.setText(String.valueOf(model.getName()));
            holder.Nickname.setText(model.getNickname());
            Log.d(TAG, "onBindViewHolder: Now Bind method executing");
           holder.v.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent i = new Intent(v.getContext(),SecondActivity.class);
                   i.putExtra("Users",getRef(position).getKey());
                   mContext.startActivity(i);

               }
           });
        }

In MainActivity:

      mAdapter = new FirebaseAdapter(options,MainActivity.this);

In SecondActivity

      public class SecondActivity extends AppCompatActivity {
            DatabaseReference ref;
         TextView gname,gnickname;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_second);
                gname = (TextView)findViewById(R.id.gettingname);
                gnickname =(TextView)findViewById(R.id.gettingnickname);

                ref = FirebaseDatabase.getInstance().getReference().child("Users");

                String Users = getIntent().getStringExtra("Users");

               ref.child(Users).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if(snapshot.exists()){
                            String name = Objects.requireNonNull(snapshot.child("Name").getValue()).toString();
                            String nick = Objects.requireNonNull(snapshot.child("Nick").getValue()).toString();

                            gname.setText(name);
                            gnickname.setText(nick);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });

Error:
  Process: com.example.firebasesearchrec, PID: 20933
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.example.firebasesearchrec.SecondActivity$1.onDataChange(SecondActivity.java:34)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)

谢谢。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

wwtsj6pe

wwtsj6pe1#

首先,获取活动上下文

Context mContext;

Get context from constructor
public FirbaseAdapter(FirebaseRecyclerOptions<DataClass> options, Context context) {
    super(options);
    this.mContext = context;

}

最后

Intent i = new Intent(view.getContext(), SecondActivity.class);
        i.putExtra("Users",getRef(position).getKey());
        mContext.startActivity(i);

或者如果你想要快速简单的解决方案,试试这个

Intent i = new Intent(view.getContext(), SecondActivity.class);
        i.putExtra("Users",getRef(position).getKey());
        view.getContext().startActivity(i);
rqenqsqc

rqenqsqc2#

对于第三种方法,请使用此方法。

v.getContext().startActivity(i);
5rgfhyps

5rgfhyps3#

请尝试使用view object v.getcontext()而不是mainactivity.this

相关问题