Android按钮返回为空

时间:2018-12-04 07:18:23

标签: java android

我有一个包含多个片段的项目,并且使用底部导航在它们之间进行切换。在我的最后一个片段中,我试图有一个按钮,可以根据上一个片段的输入来更改其文本。我使用捆绑包传输数据,效果很好,但是当我尝试将按钮的文本设置为传输的输入时,我得到了一个空指针。我进行了调试,发现无论出于何种原因,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)

3 个答案:

答案 0 :(得分:1)

这是因为您要在onCreateView中返回与此不同的视图:

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中的Button与以下视图绑定:

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

但是在方法的最后,您将给出不同的视图:

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

答案 1 :(得分:0)

更改

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

    return view2;

我认为这是因为您没有返回错误的布局,而是给其他布局充气而不是返回

答案 2 :(得分:0)

欢迎使用StackOverflow。

首先,确定要充气的布局:

R.layout.fragment_searchR.layout.fragment_result

后一个似乎很明显。

接下来,通过覆盖onViewCreated()来使用它。此方法将onCreateView()中已放大的视图。因此,您的代码看上去比当前代码清晰得多:

// best place to fetch arguments passed
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

// best place to inflate layout that you want to be displayed in Fragment
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflate(R.layout.fragment_result,fragment_container,false);
}

// best place to use the View that was inflated in onCreateView()
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    drugButton = (Button) view.findViewById(R.id.drugButt);
}