【问题标题】:Crash when I attempt to call context in a fragment当我尝试在片段中调用上下文时崩溃
【发布时间】:2023-03-15 08:07:01
【问题描述】:

logcat 说:

尝试在空对象上调用虚拟方法“...” 参考

当我将一个孩子添加到“root_view”时,我调用了上下文。看起来 getActivity() 无法正常工作。我到处搜索,但没有找到任何解决方案。也许问题是onAttach 方法?或者也许我必须在MainActivity 和片段之间创建一个类?

这是我的片段代码:

 public class Profilo extends Fragment {

    private OnFragmentInteractionListener mListener;

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

       public static Profilo newInstance(String param1, String param2) {
        Profilo fragment = new Profilo();
        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);
        }

        ArrayList<String> namesOfNumbers = new ArrayList<String>();

        namesOfNumbers.add("uno");
        namesOfNumbers.add("due");
        namesOfNumbers.add("tre");
        namesOfNumbers.add("quattro");
        namesOfNumbers.add("cinque");
        namesOfNumbers.add("sei");
        namesOfNumbers.add("sette");
        namesOfNumbers.add("otto");
        namesOfNumbers.add("nove");
        namesOfNumbers.add("dieci");


        LinearLayout root_view = (LinearLayout) getView().findViewById(R.id.linear_layout_main_profilo);
        TextView word_view_1 = new TextView(getActivity());
        word_view_1.setText(namesOfNumbers.get(0));
        root_view.addView(word_view_1);

    }

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

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            Toast.makeText(context,"NOTIFICATION FRAGMENT ATTACHED",Toast.LENGTH_SHORT).show();
        }
    }

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

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

}

【问题讨论】:

  • getactivity() 可以正常工作,请确保您在活动中使用了支持的片段管理器而不是片段管理器
  • 您可以将上下文从调用活动传递给构造函数,也可以调用 getApplicationContext()
  • 是的,我使用了 supportFragmentManager,我也尝试过 getApplicationContext()。
  • 在 onCreateView 方法中添加视图

标签: java android fragment android-context


【解决方案1】:
  • 起初RETURN查看;
  • onCreateView内致电LinearLayoutTextView

试试

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
{
        View view = inflater.inflate(R.layout.fragment_profilo, null);
        LinearLayout root_view = (LinearLayout) view .findViewById(R.id.linear_layout_main_profilo);
        TextView word_view_1 = new TextView(getActivity());
        word_view_1.setText(namesOfNumbers.get(0));
        root_view.addView(word_view_1);

        return view;
}

【讨论】:

  • 你去 :) 这家伙真快!
猜你喜欢
  • 2017-05-22
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
相关资源
最近更新 更多