Android can';t从另一个片段将项目添加到我的回收器视图
我有一个有五个片段的活动,其中一个片段是通知片段,它有一个回收器视图,在该视图中,在收到通知时应该填充通知。现在我有了一个函数,可以将项目添加到recycler视图,但是当我尝试从另一个片段添加它时,它会抛出null引用错误 这是一个片段,其中有回收器和在recyclerview上添加项目的功能Android can';t从另一个片段将项目添加到我的回收器视图,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,我有一个有五个片段的活动,其中一个片段是通知片段,它有一个回收器视图,在该视图中,在收到通知时应该填充通知。现在我有了一个函数,可以将项目添加到recycler视图,但是当我尝试从另一个片段添加它时,它会抛出null引用错误 这是一个片段,其中有回收器和在recyclerview上添加项目的功能 @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable View
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View v = inflater.inflate (R.layout.fragment_notificationcict, container, false);
notimodels = new ArrayList<> ();
RecyclerView mRecyclerView = v.findViewById (R.id.notificationsRecycler);
mRecyclerView.setHasFixedSize (true);
mLayoutManager = new LinearLayoutManager (getContext ());
mAdapter = new NotificationAdapter (notimodels);
mRecyclerView.setLayoutManager (mLayoutManager);
mRecyclerView.setAdapter (mAdapter);
}
public void addItem(NotificationModel n)
{
notimodels = new ArrayList<> ();
notimodels.add (n);
mAdapter.notifyItemInserted (notimodels.size () -1);
}
这是错误信息
rocess: com.univibezstudios.ocappservice.ocapp, PID: 27625
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView$Adapter.notifyItemInserted(int)' on a null object reference
at com.univibezstudios.ocappservice.ocapp.Fragments.NotificationFragmentCict.addItem(NotificationFragmentCict.java:99)
at com.univibezstudios.ocappservice.ocapp.Fragments.AccountFragmentCict$4$1$1.onDataChange(AccountFragmentCict.java:374)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6803)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
看,这是片段A
package com.codinginflow.fragmentcommunicationexample;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FragmentA extends Fragment {
private FragmentAListener listener;
private EditText editText;
private Button buttonOk;
public interface FragmentAListener {
void addItemsToFragmentB();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a, container, false);
editText = v.findViewById(R.id.edit_text);
buttonOk = v.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addItemsToFragmentB();
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentAListener) {
listener = (FragmentAListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
这是片段B
package com.codinginflow.fragmentcommunicationexample;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FragmentB extends Fragment {
private FragmentBListener listener;
private EditText editText;
private Button buttonOk;
public interface FragmentBListener {
addItemsToFragmentA();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b, container, false);
editText = v.findViewById(R.id.edit_text);
buttonOk = v.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addItemsToFragmentA();
}
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentBListener) {
listener = (FragmentBListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentBListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
这是一项活动,您将在其中向“回收者”视图中添加项目
package com.codinginflow.fragmentcommunicationexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
private FragmentA fragmentA;
private FragmentB fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new FragmentA();
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();
}
@Override
public void addItemsToFragmentA() {
fragmentB.updateEditText(input);
}
@Override
public void addItemsToFragmentB() {
fragmentA.updateEditText(input);
}
}
你应该和一个片段和另一个片段通信,如下面的链接。。。使用界面谢谢,但您建议的教程只是用于发送数据,但我的问题是从不同的片段访问recycler视图,并能够从那里添加项目@arunabimanyun,而不是在添加项目时创建该片段的新实例…全局保留该片段的引用…并调用addItem方法从这个角度来看,请告诉我我是一个android开发的新手,尤其是fragments@ArunAbimanyuso是什么方法,我应该在我的通知fragments界面上传递什么方法通过addItemsToFragmentB(NotificationModel NotificationModel)这样的界面传递你的值嘿,兄弟,传递什么现在让我很困惑,你能告诉我如何使用公共接口NotificationListener{void AddItems(ArrayList notificationModels);}
package com.codinginflow.fragmentcommunicationexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
private FragmentA fragmentA;
private FragmentB fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new FragmentA();
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();
}
@Override
public void addItemsToFragmentA() {
fragmentB.updateEditText(input);
}
@Override
public void addItemsToFragmentB() {
fragmentA.updateEditText(input);
}
}