getActivity()在片段上返回null?[英] getActivity() returns null on fragment?

本文是小编为大家收集整理的关于getActivity()在片段上返回null?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有MainActivity()和FirstFragment().

我正在调用MainActivity()第一范围中的函数.

问题是,时间getActivity()返回null?

推荐答案

当片段启动

时执行此操作
public void onViewCreated(View view, Bundle savedInstanceState) {
    mcontext=getContext();

如果您需要的话,McOntext将很容易被施放为活动.

自Android API 23级以来,ONATTACH(活动活动)已被弃用.您需要使用Onattach(上下文上下文). http://developer.android.com/reference/android/app/fragment.html#onattach(Android.app.Activity) 因此,如果您想使用Onattach,则需要做

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Activity a;
    if (context instanceof Activity){
         a=(Activity) context;
    }
} 

不要做

  if(getActivity()==null){

当您将应用程序移至背景并返回时,获得活动将返回null. 如果您使用它,它将有时会忽略代码的这一部分,这将创建您不会理解其来自何处的错误.

其他推荐答案

请使用Activity参考onAttach().我认为这是使用fragment

的实例的最佳pratice
 public class FirstFragment extends Fragment {
 private Activity mActivity;
     @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mActivity = (Activity) context;//use this one .. this is MainActivity instance u can use this as MainActivity mMainActivity = (MainActivity)mActivity;
        }
  }

其他推荐答案

// declare a variable activity in your fragment
private Activity activity;

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof Activity) {
        this.activity = (Activity) context;
    }

}

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


private void initToolbar() {

    // then use the var in any function
    if (activity == null) {
        return;
    }

}

本文地址:https://www.itbaoku.cn/post/2091052.html

问题描述

I have MainActivity() and a FirstFragment().

I'm calling a function which is in FirstFragment from MainActivity().

The problem is, that time getActivity() returns null ?

推荐答案

Do this when fragment start

public void onViewCreated(View view, Bundle savedInstanceState) {
    mcontext=getContext();

mcontext will easily be casted as activity if you need it.

Since Android API level 23, onAttach(Activity activity) has been deprecated. You need to use onAttach(Context context). http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity) so if you want to use onattach you need to do

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Activity a;
    if (context instanceof Activity){
         a=(Activity) context;
    }
} 

do not do

  if(getActivity()==null){

get activity will return null when you move the app to the background and return. if you use this it will just ignore this part of the code sometimes which will create bugs you won't understand where they come from.

其他推荐答案

Please use Activity reference from onAttach(). I think this is the best pratice to use instance of Activity from Fragment

 public class FirstFragment extends Fragment {
 private Activity mActivity;
     @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mActivity = (Activity) context;//use this one .. this is MainActivity instance u can use this as MainActivity mMainActivity = (MainActivity)mActivity;
        }
  }

其他推荐答案

// declare a variable activity in your fragment
private Activity activity;

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof Activity) {
        this.activity = (Activity) context;
    }

}

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


private void initToolbar() {

    // then use the var in any function
    if (activity == null) {
        return;
    }

}