简体   繁体   English

在意图android中出现错误有人尝试解决此错误并帮助我,查看SS,我尝试了三种方法来解决但没有用

[英]Getting error in intent android somebody try to solve this error and help me,Check out SS,I tried three methods to solve but no use

在此处输入图片说明 In FirebaseAdapter,I've created view for intent in ViewHolderClass在 FirebaseAdapter 中,我在 ViewHolderClass 中为意图创建了视图

     @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)

.......................Thank you............................................................................................................................................................ .......................谢谢......................... ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………………………………………………

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

v.getContext().startActivity(i);

First of all, get Activity Context首先,获取Activity Context

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

}

At last最后

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

or if you want quick & easy solution, try this或者如果你想要快速简单的解决方案,试试这个

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

    

在此处输入图片说明 In FirebaseAdapter,I've created view for intent in ViewHolderClass在FirebaseAdapter中,我已经为ViewHolderClass中的意图创建了视图

     @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)

.......................Thank you............................................................................................................................................................ .......................谢谢......................... ................................................... ................................................... ...............................

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我尝试在我的小型 android 项目中使用意向电子邮件,但它不起作用。 谁能帮我找出错误? - I tried to use intent email in my small android project but its not working. Can anyone help me to find the error? 我收到这个错误,如正文所示,在 ubuntu 中构建 apk 文件时,请帮我解决这个错误 - I am getting this error as shown in body, while building a apk file in ubuntu, please help me to solve this error 如何为 firebase 中的一个特定孩子提供参考? 如何在孩子里面阅读? 查看我的屏幕截图并帮助我解决此错误 - How to give reference for one particular child from firebase ? how to read inside child? Check out my screenshot and help me to solve this error 我尝试使用多态性,但我的代码不能像我预期的那样工作,有人可以帮我解决这个问题吗? - I try to use polymorphism but my code does not work as i excpected, could anyone help me solve this problem? 我得到这个错误..pls指导我解决它 - I m getting this error ..pls guide me to solve it 如何解决Android中的内存不足错误? - How to solve Out of Memory Error in android? 为什么我在 java 代码中遇到无法访问的语句错误。 需要帮助来解决这个错误 - why am I getting unreachable statement error in java code. need help to solve this error 当我尝试显示JSP页面时如何解决错误? - How to solve the error when I try to display JSP page? 我收到BeanFactory错误:NoSuchBeanDefinitionException,我不知道如何解决它 - I am getting BeanFactory error: NoSuchBeanDefinitionException and I can't figure out how to solve it 在Android Studio中添加解析sdk后出现错误。 我在互联网上尝试了许多解决方案,但似乎没有什么可以解决 - Got an error after adding parse sdk in android studio. I've tried many solution on internet but seems like nothing can solve
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM