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

huangapple go评论81阅读模式
英文:

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

问题

I am having a null pointer of, Is Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
I am having problems with my recyclerView
Here is my log cat Please help

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

Here is my chat activity class

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

    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 for chat activity:

<?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>
英文:

I am having a null pointer of, Is Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
I am having problems with my recyclerView
Here is my log cat Please help

    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 &#39;void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)&#39; 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 &#39;void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)&#39; 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)&#160;
at android.app.ActivityThread.-wrap12(Unknown Source:0)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)&#160;
at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
at android.os.Looper.loop(Looper.java:164)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6739)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)&#160;

Here is my chat activity class

           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&lt;Chat&gt; 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(&quot;&quot;);
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(&quot;userid&quot;);
fuser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference(&quot;users&quot;).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(&quot;&quot;)){
sendMessage(fuser.getUid(), userid, msg);
}else {
Toast.makeText(ChatActivity.this, &quot;You can&#39;t send empty message&quot;, 
Toast.LENGTH_SHORT).show();
}
text_send.setText(&quot;&quot;);
}
});
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&lt;String, Object&gt;hashMap = new HashMap&lt;&gt;();
hashMap.put(&quot;sender&quot;, sender);
hashMap.put(&quot;receiver&quot;, receiver);
hashMap.put(&quot;message&quot;, message);
reference.child(&quot;chats&quot;).push().setValue(hashMap);
}
private  void  readMessage(final String myid, final String userid){
mChat = new ArrayList&lt;&gt;();
reference = FirebaseDatabase.getInstance().getReference(&quot;chats&quot;);
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) &amp;&amp; chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) &amp;&amp; chat.getSender().equals(myid)){
mChat.add(chat);
}
messageAdapter = new MessageAdapter(ChatActivity.this, mChat);
recyclerView.setAdapter(messageAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

}
}

here is my XML for chat activity

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:background=&quot;@mipmap/images&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;include layout=&quot;@layout/layout_bottom_navigation_view&quot;/&gt;
&lt;android.support.design.widget.AppBarLayout
android:id=&quot;@+id/bar_layout&quot;
android:background=&quot;@mipmap/images&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;50dp&quot;&gt;
&lt;android.support.v7.widget.Toolbar
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/toolbar&quot;&gt;
&lt;TextView
android:id=&quot;@+id/username_recieve&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_marginStart=&quot;50dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;The lyrics&quot;
android:textStyle=&quot;bold&quot;
android:layout_marginTop=&quot;16dp&quot;
android:textSize=&quot;16sp&quot;
android:textColor=&quot;@color/black&quot;/&gt;
&lt;/android.support.v7.widget.Toolbar&gt;
&lt;/android.support.design.widget.AppBarLayout&gt;
&lt;android.support.v7.widget.RecyclerView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_below=&quot;@id/bar_layout&quot;
android:layout_above=&quot;@+id/bottom&quot;
android:layout_centerHorizontal=&quot;true&quot;/&gt;
&lt;RelativeLayout
android:id=&quot;@+id/bottom&quot;
android:layout_width=&quot;match_parent&quot;
android:padding=&quot;5dp&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:background=&quot;#FDD5E3&quot;
android:layout_marginBottom=&quot;50dp&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/text_send&quot;
android:layout_toLeftOf=&quot;@+id/btn_send&quot;
android:layout_toRightOf=&quot;@id/btn_camera&quot;
android:background=&quot;@color/grey&quot;
android:layout_centerVertical=&quot;true&quot;
android:hint=&quot;Write your message&quot;/&gt;
&lt;ImageButton
android:id=&quot;@+id/btn_send&quot;
android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;40dp&quot;
android:background=&quot;#FDD5E3&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_alignParentRight=&quot;true&quot;
android:src=&quot;@drawable/ic_arrow&quot; /&gt;
&lt;ImageButton
android:id=&quot;@+id/btn_camera&quot;
android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;40dp&quot;
android:background=&quot;#FDD5E3&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentLeft=&quot;true&quot;
android:src=&quot;@drawable/ic_camera&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;/RelativeLayout&gt;

答案1

得分: 0

问题出在XML文件上,我没有在recyclerview上指定id。

英文:

The problem is on the XML file I did not indicate the id on the recyclerview

答案2

得分: 0

你忘记为 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"/>
英文:

You forget to add id for RecyclerView. Add android:id=&quot;@+id/recycler_view&quot;:

 &lt;android.support.v7.widget.RecyclerView
android:id=&quot;@+id/recycler_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_below=&quot;@id/bar_layout&quot;
android:layout_above=&quot;@+id/bottom&quot;
android:layout_centerHorizontal=&quot;true&quot;/&gt;

huangapple
  • 本文由 发表于 2020年10月14日 03:49:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64342218.html
匿名

发表评论

匿名网友

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

确定