我正在做一个涉及片段的小型android项目..我的log cat显示空指针异常,因为上下文值为null ..
这是我的代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.syllabusselection, container, false);
main(rootView);
return rootView;
}
public void main(View rootView)
{
TextView textyear,textsubject,textschema,textPowered ;
final Context context = getActivity();
Typeface Roboto = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Light.ttf");
Typeface Nexa = Typeface.createFromAsset(context.getAssets(),"fonts/nexa/Nexa_Light.ttf");//Nexa_Light.ttf Nexa_Bold.ttf
textsubject = (TextView)rootView.findViewById(R.id.textsubject);
textschema = (TextView)rootView.findViewById(R.id.textscheme);
textPowered = (TextView)rootView.findViewById(R.id.textPowered);
最初是
final Context context = getActivity();
是null我把它改为getActivity()但是我仍然得到空指针excewption ..那么什么是错误?
这是我的日志猫
03-10 03:30:06.015: E/AndroidRuntime(1218): FATAL EXCEPTION: main
03-10 03:30:06.015: E/AndroidRuntime(1218): Process: info.androidhive.tabsswipe, PID: 1218
03-10 03:30:06.015: E/AndroidRuntime(1218): java.lang.NullPointerException
03-10 03:30:06.015: E/AndroidRuntime(1218): at info.androidhive.tabsswipe.Question.main(Question.java:63)
03-10 03:30:06.015: E/AndroidRuntime(1218): at info.androidhive.tabsswipe.Question.onCreateView(Question.java:55)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-10 03:30:06.015: E/AndroidRuntime(1218): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-10 03:30:06.015: E/AndroidRuntime(1218): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-10 03:30:06.015: E/AndroidRuntime(1218): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
答案 0 :(得分:3)
将您main
的调用移至片段的onActivityCreated
。这意味着您应该将rootView.findViewById
来电转移到onCreateView
,并在Typeface
中保留main
来电。
答案 1 :(得分:0)
我想当你尝试获取上下文时,片段还没有附加到Activity。所以你应该重新考虑应用逻辑。正如@Vino指出的那样,你应该使用另一个回调。