Я пытаюсь получить групповое имя из фрагмента в групповой чат. Idk. Что не так, я делаю здесь, когда я нажимаю на любое представление списка, которое показывает мне
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
, вот мой GroupsFragment. java
public class GroupsFragment extends Fragment {
private View groupfragmentview;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList <String> list_of_grps = new ArrayList<>();
private DatabaseReference databaseReference;
public GroupsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
groupfragmentview = inflater.inflate(R.layout.fragment_groups, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Groups");
fetchanddisplaygroups();
initializedfields();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String current_grp_name = adapterView.getItemAtPosition(position).toString();
Intent intent = new Intent(getContext(),GroupChatActivity.class);
intent.putExtra( "Group_name " , current_grp_name);
startActivity(intent);
}
});
return groupfragmentview;
}
private void initializedfields() {
listView = groupfragmentview.findViewById(R.id.grp_list_view);
arrayAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,list_of_grps);
listView.setAdapter(arrayAdapter);
}
private void fetchanddisplaygroups() {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<>();
Iterator iterator = dataSnapshot.getChildren().iterator();
while (iterator.hasNext()){
set.add(((DataSnapshot)iterator.next()).getKey());
}
list_of_grps.clear();
list_of_grps.addAll(set);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
и вот мой activity_group_chat. xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.GroupChatActivity">
<include
android:id="@+id/grp_chat_bar_layout"
layout="@layout/app_bar_layout">
</include>
<ScrollView
android:id="@+id/my_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/my_linear_layout"
android:layout_below="@+id/grp_chat_bar_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/grp_chat_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="50dp"
android:text="Send Message"
android:textAllCaps="false"
android:textColor="@android:color/darker_gray"
android:textSize="24sp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<EditText
android:id="@+id/input_grp_message"
android:layout_width="355dp"
android:layout_height="wrap_content"
android:hint="Write your message"
android:padding="17dp"
android:textColor="@android:color/darker_gray" />
<ImageButton
android:id="@+id/send_message_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/email" />
</LinearLayout>
и вот group_chat_activity
public class GroupChatActivity extends AppCompatActivity {
private Toolbar toolbar;
private ImageButton send_message_btn;
private EditText input_grp_message;
private TextView grp_chat_textview;
private ScrollView my_scroll_view;
private String current_grp_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_chat);
//all id's here
toolbar = findViewById(R.id.grp_chat_bar_layout);
send_message_btn = findViewById(R.id.send_message_btn);
input_grp_message = findViewById(R.id.input_grp_message);
grp_chat_textview = findViewById(R.id.grp_chat_textview);
my_scroll_view = findViewById(R.id.my_scroll_view);
current_grp_name = getIntent().getExtras().get("Group_name").toString();
Toast.makeText(GroupChatActivity.this,"" + current_grp_name,Toast.LENGTH_SHORT).show();
setSupportActionBar(toolbar);
// getSupportActionBar().setTitle(current_grp_name);
}