运行时异常:在Fragment添加内容之前必须调用requestFeature()

时间:2014-11-28 06:33:47

标签: android android-fragments nullpointerexception

我收到空指针异常。我想在Fragments not Activity中使用此功能getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

堆栈跟踪:

11-28 01:18:54.599: E/AndroidRuntime(1699): FATAL EXCEPTION: main
11-28 01:18:54.599: E/AndroidRuntime(1699): Process: info.androidhive.tabsswipe, PID: 1699
11-28 01:18:54.599: E/AndroidRuntime(1699): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
11-28 01:18:54.599: E/AndroidRuntime(1699):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at info.androidhive.tabsswipe.TopRatedFragment.onCreateView(TopRatedFragment.java:26)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.View.measure(View.java:16497)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.View.measure(View.java:16497)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.View.measure(View.java:16497)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.View.measure(View.java:16497)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.View.measure(View.java:16497)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.Choreographer.doFrame(Choreographer.java:544)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.os.Handler.handleCallback(Handler.java:733)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.os.Looper.loop(Looper.java:136)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at java.lang.reflect.Method.invoke(Method.java:515)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-28 01:18:54.599: E/AndroidRuntime(1699):     at dalvik.system.NativeStart.main(Native Method)

TopRatedFragment.java:

public class TopRatedFragment extends Fragment implements {

    private float mActionBarHeight;
    private ActionBar actionBar;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); --->Null pointer exception

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

        final TypedArray styledAttributes = getActivity().getTheme().obtainStyledAttributes(
                new int[] { android.R.attr.actionBarSize });
        mActionBarHeight = styledAttributes.getDimension(0, 0);
        styledAttributes.recycle(); 
        actionBar = getActivity().getActionBar();  



        ((ScrollView)rootView.findViewById(R.id.parent)).getViewTreeObserver().addOnScrollChangedListener(this);




        return rootView;
    }

我不知道如何在Fragment的getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);中添加此功能并解决此异常。

1 个答案:

答案 0 :(得分:2)

您无法在申请过程中执行此操作。 你必须在setContentView(R.layout.my_activity)之前在FragmentActivity onCreate中添加getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);行;

或在您的style.xml中将<item name="android:windowActionBarOverlay">true</item>添加到您的应用主题

或我最喜欢的使用工具栏 f.e 进入my_activity.xml添加

<android.support.v7.widget.Toolbar
   android:id="@+id/toolbar"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:minHeight="?attr/actionBarSize"
   app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   android:elevation="5dp"
/>

在你的ActionBarActivity中使用像这样的工具栏

setContentView(R.layout.my_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Theese方法只有在Android Studio中使用android-support-lib时才能使用此行添加到gradle.build中的依赖项大括号中,以便将google支持lib添加到项目中

compile 'com.android.support:appcompat-v7:+'

在工具栏方法中,您可以将工具栏(操作栏)控制为一个简单的视图 - 在运行时更改翻译layoutparams而不会头疼!