即使在调用onViewCreated之后,getView也为null

时间:2016-02-09 09:22:37

标签: android android-fragments

我有一个main activity和三个片段frag1frag2frag3。我正试图将值从frag3传递给frag2。为此,我创建了一个界面 该接口由main activity实现,如下面的代码所示。我现在面临的问题是,尽管创建了frag2并且它已经创建了它 view is created也是,当值从frag3到达frag2 *getView() method returns null*

以下logcat输出显示frag2的视图已创建

  

为什么frag2的getview为空?

mainactivity

public class MainActivity extends AppCompatActivity implements IValuePasser {
    private final String TAG = this.getClass().getSimpleName();

    private Toolbar mTB = null;
    private TabLayout mTL = null;
    private ViewPager mVP = null;
    private VPagerAdapter mVPAdapter = null;

    private Frag_2 mFrag2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w(TAG, "onCreate()");

        mFrag2 = new Frag_2();
    }
    ...
    ...
    ...
    @Override
    public void onValueChanged(int value) {
        this.mFrag2.fromFrag3(value);
    }
}

frag3

public class Frag_3 extends Fragment {
    private final String TAG = this.getClass().getSimpleName();

    private View mRootView = null;
    private TextView mtvText = null;
    private ATIncr mATIncr = null;

    IValuePasser mValPasser = null;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.w(TAG, "onAttach()");

        Log.v(TAG, "getView(): " + getView());
        this.mValPasser = (IValuePasser) context;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w(TAG, "onCreate()");

        this.mATIncr = new ATIncr();
        Log.v(TAG, "getView(): " + getView());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.w(TAG, "onCreateView()");

        this.mRootView = inflater.inflate(R.layout.frag_3, container, false);
        this.mtvText = (TextView) this.mRootView.findViewById(R.id.frag3_tv);

        Log.v(TAG, "getView(): " + getView());
        return mRootView;
    }
    ...
    ...
    ...

    @Override
    public void onResume() {
        super.onResume();
        Log.w(TAG, "onResume()");

        this.mATIncr.execute();

        Log.v(TAG, "getView(): " + getView());
    }

    private class ATIncr extends AsyncTask <Void, Integer, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.w(TAG, "onPreExecute()");
        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.w(TAG, "doInBackground()");

            for (int i = 0; i <= 20; i++) {
                publishProgress(i);
                SystemClock.sleep(1000);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Log.w(TAG, "onPostExecute()");
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            Log.w(TAG, "onProgressUpdate()");

            mtvText.setText("" + values[0]);
            mValPasser.onValueChanged(values[0]);
        }
    }
}

frag2

public class Frag_2 extends Fragment {
    private final String TAG = this.getClass().getSimpleName();

    private View mRootView = null;
    private TextView mtvText = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.w(TAG, "onCreateView()");

        this.mRootView = inflater.inflate(R.layout.frag_2, container, false);
        this.mtvText = (TextView) this.mRootView.findViewById(R.id.frag2_tv);

        Log.v(TAG, "getView(): " + getView());
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.w(TAG, "onViewCreated()");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.w(TAG, "onActivityCreated()");
    }
    ...
    ...
    ...
    public void fromFrag3(int value) {
        if (getView() != null) {
            this.mtvText.setText("" + value);
        } else {
            Log.d(TAG, "getViwe(): " + getView());
        }
    }
}

logcat的

02-09 10:08:25.050 25092-25092/? W/Frag_1: onAttach()
02-09 10:08:25.050 25092-25092/? W/Frag_1: onCreate()
02-09 10:08:25.050 25092-25092/? W/Frag_1: onCreateView()
02-09 10:08:25.060 25092-25092/? W/Frag_1: onViewCreated()
02-09 10:08:25.060 25092-25092/? W/Frag_1: onActivityCreated()
02-09 10:08:25.060 25092-25092/? W/Frag_1: onStart()
02-09 10:08:25.060 25092-25092/? W/Frag_1: onResume()
02-09 10:08:25.060 25092-25092/? W/Frag_2: onAttach()
02-09 10:08:25.060 25092-25092/? W/Frag_2: onCreate()
02-09 10:08:25.060 25092-25092/? W/Frag_2: onCreateView()
02-09 10:08:25.060 25092-25092/? V/Frag_2: getView(): null
02-09 10:08:25.060 25092-25092/? W/Frag_2: onViewCreated()
02-09 10:08:25.060 25092-25092/? W/Frag_2: onActivityCreated()
02-09 10:08:25.060 25092-25092/? W/Frag_3: onAttach()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): null
02-09 10:08:25.060 25092-25092/? W/Frag_3: onCreate()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): null
02-09 10:08:25.060 25092-25092/? W/Frag_3: onCreateView()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): null
02-09 10:08:25.060 25092-25092/? W/Frag_3: onViewCreated()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): android.widget.RelativeLayout{18d55403 V.E..... ......I. 0,0-0,0}
02-09 10:08:25.060 25092-25092/? W/Frag_3: onActivityCreated()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): android.widget.RelativeLayout{18d55403 V.E..... ......I. 0,0-0,0}
02-09 10:08:25.060 25092-25092/? W/Frag_2: onStart()
02-09 10:08:25.060 25092-25092/? W/Frag_2: onResume()
02-09 10:08:25.060 25092-25092/? W/Frag_3: onStart()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): android.widget.RelativeLayout{18d55403 V.E..... ......I. 0,0-0,0}
02-09 10:08:25.060 25092-25092/? W/Frag_3: onResume()
02-09 10:08:25.060 25092-25092/? W/Frag_3: onPreExecute()
02-09 10:08:25.060 25092-25092/? V/Frag_3: getView(): android.widget.RelativeLayout{18d55403 V.E..... ......I. 0,0-0,0}
02-09 10:08:25.070 25092-25111/? W/Frag_3: doInBackground()

当frag3传递给frag2时的logcat

02-09 10:08:29.080 25092-25092/? W/Frag_3: onProgressUpdate()
02-09 10:08:29.080 25092-25092/? D/Frag_2: getViwe(): null
02-09 10:08:30.080 25092-25092/? W/Frag_3: onProgressUpdate()
 02-09 10:08:30.080 25092-25092/? D/Frag_2: getViwe(): null
02-09 10:08:30.140 308-308/? E/SMD: DCD ON
02-09 10:08:31.090 25092-25092/? W/Frag_3: onProgressUpdate()
02-09 10:08:31.090 25092-25092/? D/Frag_2: getViwe(): null
02-09 10:08:32.090 25092-25092/? W/Frag_3: onProgressUpdate()
02-09 10:08:32.090 25092-25092/? D/Frag_2: getViwe(): null
02-09 10:08:32.920 795-4291/? D/SSRM:n: SIOP:: AP = 340, PST = 345, CUR =   450
02-09 10:08:33.080 25092-25092/? W/Frag_3: onProgressUpdate()
02-09 10:08:33.080 25092-25092/? D/Frag_2: getViwe(): null

1 个答案:

答案 0 :(得分:0)

您在主private Frag_2 mFrag2 = null;中将其设置为null,并从this.mFrag2.fromFrag3(value);调用它。 mFrag2也未在onCreate()中初始化。现在它为null,您尝试使用fromFrag3(value)赋予值。让我们看看fromFrag3。它检查其视图是否为空。您发送空视图并在此if语句中传递setText:

if (getView() != null) {
    this.mtvText.setText("" + value);
} else {
    Log.d(TAG, "getViwe(): " + getView());
}

您必须从mFrag2中删除空值。