簡體   English   中英

我有兩個空指針嘗試調用虛擬方法 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)

[英]I am having a two null pointer Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)

我有一個空指針,Is Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference 我的recyclerView有問題這是我的日志貓請幫忙

    2020-10-13 21:42:16.265 12483-12483/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: umo.com.players, PID: 12483
java.lang.RuntimeException: Unable to start activity ComponentInfo{umo.com.players/umo.com.players.Home.ChatActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2868)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
    at android.app.ActivityThread.-wrap12(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6739)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
    at umo.com.players.Home.ChatActivity.onCreate(ChatActivity.java:84)
    at android.app.Activity.performCreate(Activity.java:7045)
    at android.app.Activity.performCreate(Activity.java:7036)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1217)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2815)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958) 
    at android.app.ActivityThread.-wrap12(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6739) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

這是我的聊天活動課

           public class ChatActivity extends AppCompatActivity {

private ImageButton btn_send, btn_camera;
private RecyclerView usermessageslist;
private EditText text_send;
private BottomNavigationViewEx bottomNavigationViewEx;
private TextView recieverName;
     //    private CircleImageView recieverprofileimage;
private String messageRecieverID, messageRecieverName;
FirebaseUser fuser;
DatabaseReference reference;


MessageAdapter messageAdapter;
List<Chat> mChat;

RecyclerView recyclerView;

Intent intent;

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

       // messageRecieverID = getIntent().getExtras().get(field_user_id).toString();
      //
     //        ActionBar actionBar = getSupportActionBar();
     //        actionBar.setDisplayHomeAsUpEnabled(true);
    //        actionBar.setDisplayShowCustomEnabled(true);
    //        LayoutInflater layoutInflater = (LayoutInflater) 
      this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   //        View action_bar_view = layoutInflater.inflate(R.layout.user_item,null);
   //        actionBar.setCustomView(action_bar_view);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(linearLayoutManager);

    intent = getIntent();
    final String userid = intent.getStringExtra("userid");
    fuser = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("users").child(userid);
    btn_send = findViewById(R.id.btn_send);
    recieverName = findViewById(R.id.username_recieve);
    btn_camera = findViewById(R.id.btn_camera);
    text_send = findViewById(R.id.text_send);
    btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String msg = text_send.getText().toString();
            if (!msg.equals("")){
                sendMessage(fuser.getUid(), userid, msg);
            }else {
                Toast.makeText(ChatActivity.this, "You can't send empty message", 
    Toast.LENGTH_SHORT).show();
            }
            text_send.setText("");
        }
    });



    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            recieverName.setText(user.getUsername());
            readMessage(fuser.getUid(), userid);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
  }

   private void sendMessage(String sender, String receiver, String message) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    HashMap<String, Object>hashMap = new HashMap<>();
    hashMap.put("sender", sender);
    hashMap.put("receiver", receiver);
    hashMap.put("message", message);

    reference.child("chats").push().setValue(hashMap);

}
    private  void  readMessage(final String myid, final String userid){
    mChat = new ArrayList<>();

    reference = FirebaseDatabase.getInstance().getReference("chats");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mChat.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Chat chat = snapshot.getValue(Chat.class);
                if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
                chat.getReceiver().equals(userid) && chat.getSender().equals(myid)){
                    mChat.add(chat);
                }
                messageAdapter = new MessageAdapter(ChatActivity.this, mChat);
                recyclerView.setAdapter(messageAdapter);
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

} }

這是我的聊天活動 XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@mipmap/images"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <include layout="@layout/layout_bottom_navigation_view"/>



    <android.support.design.widget.AppBarLayout
     android:id="@+id/bar_layout"
     android:background="@mipmap/images"
     android:layout_width="match_parent"
     android:layout_height="50dp">

    <android.support.v7.widget.Toolbar
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/toolbar">

    <TextView
       android:id="@+id/username_recieve"
       android:layout_width="match_parent"
       android:layout_marginStart="50dp"
       android:layout_height="wrap_content"
       android:text="The lyrics"
       android:textStyle="bold"
       android:layout_marginTop="16dp"
       android:textSize="16sp"
       android:textColor="@color/black"/>

     </android.support.v7.widget.Toolbar>
     </android.support.design.widget.AppBarLayout>

     <android.support.v7.widget.RecyclerView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@id/bar_layout"
      android:layout_above="@+id/bottom"
      android:layout_centerHorizontal="true"/>


   <RelativeLayout
     android:id="@+id/bottom"
     android:layout_width="match_parent"
     android:padding="5dp"
     android:layout_alignParentBottom="true"
     android:background="#FDD5E3"
     android:layout_marginBottom="50dp"
     android:layout_height="wrap_content">

     <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_send"
        android:layout_toLeftOf="@+id/btn_send"
        android:layout_toRightOf="@id/btn_camera"
        android:background="@color/grey"
        android:layout_centerVertical="true"
        android:hint="Write your message"/>

     <ImageButton
        android:id="@+id/btn_send"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#FDD5E3"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_arrow" />

     <ImageButton
        android:id="@+id/btn_camera"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#FDD5E3"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_camera" />

    </RelativeLayout>

    </RelativeLayout>

問題出在XML文件上我沒有在recyclerview上注明id

您忘記為RecyclerView添加 id 。 添加android:id="@+id/recycler_view"

 <android.support.v7.widget.RecyclerView
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_below="@id/bar_layout"
  android:layout_above="@+id/bottom"
  android:layout_centerHorizontal="true"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM