在片段的onActivityCreated中设置textview文本会导致崩溃

时间:2017-08-21 17:21:27

标签: android android-fragments

我的活动中有一个不可见的片段,应该在创建容器活动后从两个textview的首选项中设置文本。我在onActivityCreated中执行此操作,如下所示:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    //Get new strings for name and motto if any
    mName = mPrefs.getString(getString(R.string.pref_name) , getString(R.string.your_name));
    mMotto = mPrefs.getString(
            getString(R.string.pref_motto),
            getString(R.string.your_motto)
    );

    //Find name and motto text views
    TextView yourName = getActivity().findViewById(R.id.tv_your_name);
    TextView yourMotto = getActivity().findViewById(R.id.tv_your_motto);
    //Set name and motto text views
    yourName.setText(mName);
    yourMotto.setText(mMotto);
}

两个文本视图在导航标题内定义,导航标题包含在导航抽屉android:headerLayout中。反过来,导航抽屉在MainActivity(包含我的片段的活动)中初始化。但是,应用程序崩溃时出现以下错误。我试图在onCreateView中设置文本视图但它同样崩溃了。

       --------- beginning of crash
08-21 17:03:27.311 4068-4068/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mehdi.memo, PID: 4068
    java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.mehdi.memo/com.mehdi.memo.MainActivity}: 
    java.lang.NullPointerException: Attempt to invoke virtual method 'void 
    android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    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:767)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.mehdi.memo.AlarmFragment.onActivityCreated(AlarmFragment.java:145)
    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2346)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1442)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3171)
    at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
    at android.app.Activity.performStart(Activity.java:6992)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    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:767) 

1 个答案:

答案 0 :(得分:0)

这是因为它无法找到textview。在片段中分配视图的最佳做法是onCreateViewonViewCreate方法。请参考..

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.your_fragment_layout, container, false);
   }

 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState); 
    mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    //Get new strings for name and motto if any
    mName = mPrefs.getString(getString(R.string.pref_name) , getString(R.string.your_name));
    mMotto = mPrefs.getString(
        getString(R.string.pref_motto),
        getString(R.string.your_motto)
    );

    //finding textviews from your layout file and assigning values

    TextView yourName = view.findViewById(R.id.tv_your_name);
    TextView yourMotto = view.findViewById(R.id.tv_your_motto);
    //Set name and motto text views
    yourName.setText(mName);
    yourMotto.setText(mMotto);
}