英文:
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 '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) 
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);
// 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) {
}
});
}
}
here is my 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>
答案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="@+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"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论