我使用Fragment在TabLayout中保存我的内容。 在里面,我有Button。当我尝试在Fragment类中实例化时,我得到以下错误:
var idx = sheet
.Cells["1:1"]
.First(c => c.Value.ToString() == "Birthdate")
.Start
.Column;
sheet.Column(idx).Style.Numberformat.Format = "mm-dd-yy";
以下是 onCreateView(...)
中的代码com.example.fanjavaid.tablayout E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.fanjavaid.tablayout.fragment.AccountFragment.onCreateView(AccountFragment.java:27)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
这是我的Fragment XML布局:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
RelativeLayout mFragmentAccount = (RelativeLayout) getActivity().findViewById(R.id.fragment_account);
mBtnAddPerson = (Button) mFragmentAccount.findViewById(R.id.add_person);
Typeface font = Typeface.createFromAsset(mFragmentAccount.getContext().getAssets(), "/fonts/Rubik-Regular.ttf");
mBtnAddPerson.setTypeface(font);
return inflater.inflate(R.layout.fragment_account, mFragmentAccount,true);
}
我错过了什么?
谢谢
答案 0 :(得分:1)
看起来你试图在充气之前找到它们。 首先给视图充气,然后访问它的孩子。
请试试这个
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View mFragmentAccount = inflater.inflate(R.layout.fragment_account, container,true);
mBtnAddPerson = (Button) mFragmentAccount.findViewById(R.id.add_person);
Typeface font = Typeface.createFromAsset(mFragmentAccount.getContext().getAssets(), "/fonts/Rubik-Regular.ttf");
mBtnAddPerson.setTypeface(font);
return mFragmentAccount;
}
答案 1 :(得分:1)
您正试图从您的活动中获取您的视图。你必须给你的片段充气。
View rootView = inflater.inflate(R.id.fragment_account, container, false);
答案 2 :(得分:0)
View
之前尝试访问View
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_account, mFragmentAccount,true);
}
最好在View
中添加访问onViewCreated(...)
的代码,如下所示:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RelativeLayout mFragmentAccount = (RelativeLayout) getActivity().findViewById(R.id.fragment_account);
mBtnAddPerson = (Button) mFragmentAccount.findViewById(R.id.add_person);
Typeface font = Typeface.createFromAsset(mFragmentAccount.getContext().getAssets(), "/fonts/Rubik-Regular.ttf");
mBtnAddPerson.setTypeface(font);
}
快乐的编码!