我已经搜索并搜索了这个问题并尝试了许多不同的解决方案,但似乎没有任何工作。
我尝试访问我的片段布局中的对象,但是它变为null并且我意识到我的OnFragmentInteractionListener为null,这使我相信它没有吸引正确的片段。
这几乎是来自应用程序的另一个活动的复制粘贴,并且该活动完美无缺。出于某种原因,这个人只是不想沟通。
TLDR:片段中的mListener(negativeThoughtView.java)为空
我只包含了缩短代码的重要部分:
活动(JournalView.java)
public class JournalView extends AppCompatActivity implements negativeThoughtView.OnFragmentInteractionListener, distortionView.OnFragmentInteractionListener, challengingThoughtView.OnFragmentInteractionListener {
TabLayout thoughtsTab;
ViewPager viewPager;
PagerAdapterView adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_journal_view);
thoughtsTab = (TabLayout) findViewById(R.id.thoughtsViewTabs);
thoughtsTab.addTab(thoughtsTab.newTab().setText("Negative Thoughts"));
thoughtsTab.addTab(thoughtsTab.newTab().setText("Distortions"));
thoughtsTab.addTab(thoughtsTab.newTab().setText("Challenging Thoughts"));
thoughtsTab.setTabGravity(thoughtsTab.GRAVITY_FILL);
adapter = new PagerAdapterView(getSupportFragmentManager(), thoughtsTab.getTabCount());
viewPager = (ViewPager) findViewById(R.id.thoughtsViewPager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(thoughtsTab));
thoughtsTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
negativeThoughtView NegativeThoughtView =(negativeThoughtView) adapter.instantiateItem(viewPager, 0);
distortionView DistortionView =(distortionView) adapter.instantiateItem(viewPager, 1);
challengingThoughtView ChallengingThoughtView =(challengingThoughtView) adapter.instantiateItem(viewPager, 2);
if (NegativeThoughtView != null) {
String negativeThoughts = "Tester";
NegativeThoughtView.onButtonPressed(negativeThoughts);
}
if (ChallengingThoughtView != null) {
String challengingThoughts= "Tester";
ChallengingThoughtView.onButtonPressed(challengingThoughts);
}
if (DistortionView != null) {
String distortions= "Tester";
DistortionView.onButtonPressed(distortions);
}
}
@Override
public void onFragmentInteraction(Uri uri) {
}
@Override
public void negativeThoughtView(String string) {
}
@Override
public void challengingThoughtView(String string) {
}
@Override
public void distortionView(String string) {
}
}
片段(negativeThoughtsView.java)
public class negativeThoughtView extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
TextView negativeThoughtEntry;
private OnFragmentInteractionListener mListener;
public negativeThoughtView() {
// Required empty public constructor
}
public static negativeThoughtView newInstance(String param1, String param2) {
negativeThoughtView fragment = new negativeThoughtView();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_negative_thought_view, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
negativeThoughtEntry = (TextView) getView().findViewById(R.id.negativeThoughtEntry3);
}
public void onButtonPressed(String negativeThought) {
Log.d("Passer","1");
if (mListener != null) {
Log.d("Passer","2");
negativeThoughtEntry.setText(negativeThought);
//mListener.negativeThoughtView(negativeThought);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
Log.d("Passer","mListener good");
} else {
Log.d("Passer","mListener null");
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
void negativeThoughtView(String string);
}
}