gpt4 book ai didi

java - Android 按钮返回 null

转载 作者:行者123 更新时间:2023-12-02 10:31:08 26 4
gpt4 key购买 nike

我有一个包含多个 fragment 的项目,我使用底部导航在它们之间切换。在我的最后一个 fragment 中,我试图有一个按钮可以根据前一个 fragment 的输入更改其文本。我使用 bundle 来传输数据,效果很好,但是当我尝试将按钮的文本设置为传输的输入时,我得到一个空指针。我已经调试并发现,无论出于何种原因,Android Studio 都会将按钮设置为 null,即使声明正确。我对 Android 还很陌生,因此我们将不胜感激。

public class Result extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";


private String mParam1;
private String mParam2;
private Button drugButton;

private OnFragmentInteractionListener mListener;

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

public static Result newInstance(String param1, String param2) {
Result fragment = new Result();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}

}

public View onCreateView(LayoutInflater inflater, ViewGroup fragment_container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view2 = inflater.inflate(R.layout.fragment_search,fragment_container,false);
Bundle args = getArguments();
drugButton = (Button) view2.findViewById(R.id.drugButt);
System.out.println(drugButton);
if(args != null)
{
String drug = args.getString("resultKey");
drugButton.setText(drug);
}

return inflater.inflate(R.layout.fragment_result, fragment_container, false);
}

public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}

这是我的错误消息。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alurk.marnpp, PID: 8735
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at com.example.alurk.marnpp.Result$override.onCreateView(Result.java:57)
at com.example.alurk.marnpp.Result$override.access$dispatch(Unknown Source:116)
at com.example.alurk.marnpp.Result.onCreateView(Unknown Source:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

最佳答案

这是因为您在 onCreateView 中返回了不同的 View :

public View onCreateView(LayoutInflater inflater, ViewGroup fragment_container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view2 = inflater.inflate(R.layout.fragment_search,fragment_container,false);
Bundle args = getArguments();
drugButton = (Button) view2.findViewById(R.id.drugButt);
System.out.println(drugButton);
if(args != null)
{
String drug = args.getString("resultKey");
drugButton.setText(drug);
}

return inflater.inflate(R.layout.fragment_result, fragment_container, false);
}

你应该这样做:

public View onCreateView(LayoutInflater inflater, ViewGroup fragment_container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view2 = inflater.inflate(R.layout.fragment_search,fragment_container,false);


...

return view2;

}
<小时/>

发生错误的原因是您将 onCreateView 中的按钮与以下 View 绑定(bind):

View view2 = inflater.inflate(R.layout.fragment_search,fragment_container,false);

但是你在方法的末尾给出了不同的 View :

return inflater.inflate(R.layout.fragment_result, fragment_container, false);

关于java - Android 按钮返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53607599/

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