gpt4 book ai didi

java - 使用 fragment 事务的 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:08 25 4
gpt4 key购买 nike

我从 android 开始,对多个 fragment 的实现感到疯狂。我的 MainActivity 中有两个 fragment :QuestionDetail 和 QuestionListFragment。当我使用 fragment 事务替换 QuestionDetail fragment 时,当有人单击 QuestionListFragment 选项之一时,我收到 NullPointerException 错误。

我几个小时以来一直在尝试解决这个问题。非常感谢任何帮助。

问题详情

public class QuestionListFragment extends Fragment{

private RecyclerView recView;
public static QuestionAdapter adapter;
private QuestionListener listener;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_question_list, container, false);
recView = (RecyclerView) v.findViewById(R.id.recView);
return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recView.setLayoutManager(layoutManager);

adapter = new QuestionAdapter(QuestionCatalog.getQuestionCatalog().getQuestionList().getList());
adapter.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (listener!=null) {
listener.onQuestionListener(adapter.getItem(recView.getChildAdapterPosition(v)));
}
}
});
recView.setAdapter(adapter);
}

public interface QuestionListener {
void onQuestionListener(Question q);
}

public void setQuestionListener(QuestionListener listener) {
this.listener=listener;
}

问题详情

public class QuestionDetail extends Fragment {
// We use an ID to know which Question we're using
private long questionId;

public QuestionDetail() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_question_detail, container, false);
}

@Override
public void onStart() {
super.onStart();
//Fragments don’t include findViewById(). To get a reference to a view, we use getView()
View view = getView();
if (view != null) {
TextView title = (TextView) view.findViewById(R.id.textQuestion);
Question question = QuestionCatalog.getQuestionCatalog().getQuestionList().getList().get((int) questionId);
title.setText(question.getName());
TextView kind = (TextView) view.findViewById(R.id.textKind);
kind.setText(question.getKind());
}
}

public void showDetails(String texto) {
TextView txtName = (TextView)getView().findViewById(R.id.textQuestion);
TextView txtKind = (TextView)getView().findViewById(R.id.textKind);
txtName.setText(texto);
txtKind.setText(texto);
}
}

主要 Activity

public class MainActivity extends AppCompatActivity implements QuestionListFragment.QuestionListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

QuestionListFragment frg =(QuestionListFragment)getFragmentManager().findFragmentById(R.id.listFragment);
frg.setQuestionListener(this);
}

@Override
public void onQuestionListener(Question q) {
Toast.makeText(this, "num: " + q.getName(),
Toast.LENGTH_LONG).show();
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
QuestionDetail f1 = new QuestionDetail();
f1.showDetails(q.getName());
t.replace(R.id.detailFragment,f1);
t.addToBackStack(null);
t.commit();
}

堆栈跟踪

08-04 09:54:11.914 22277-22277/com.hfad.predictandwin E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.hfad.predictandwin.Fragments.QuestionDetail.showDetails(QuestionDetail.java:50)
at com.hfad.predictandwin.MainActivity.onQuestionListener(MainActivity.java:34)
at com.hfad.predictandwin.Fragments.QuestionListFragment$1.onClick(QuestionListFragment.java:55)
at com.hfad.predictandwin.QuestionAdapter.onClick(QuestionAdapter.java:61)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

首先,不要在 onStart() 中使用 getView() - 此时您不应该获得 View 。其次,您不应该从其他地方直接调用 Fragment 实例上的方法,在您的例子中为 f1.showDetails(q.getName());。再次,Fragment 没有 View。请参阅Best practice for instantiating a new Android Fragment了解如何在将 Fragment 加载到 FragmentManager 之前将参数传递到 Fragment。

<小时/>

下一步 - 您应该使用 onCreateView 内部的 findViewById 初始化 View ,就像您对其他 Fragment 类所做的那样。例如,

private TextView txtName, txtKind;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_question_detail, container, false);

txtName = (TextView) rootView.findViewById(R.id.textQuestion);
txtKind = (TextView) rootView.findViewById(R.id.textKind);

// TODO: Read the link how to use these
Bundle arguments = getArguments();

Question question = QuestionCatalog.getQuestionCatalog()
.getQuestionList().getList().get((int) questionId);
showQuestion(question);

return view;
}

private void showQuestion(Question q) {
txtName.setText(q.getName());
txtKind.setText(q.getKind());
}

关于java - 使用 fragment 事务的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com