Fragment中的getView()导致抛出异常,不确定原因

时间:2013-04-26 12:49:08

标签: java android view android-fragments

我有以下代码:

    package com.example.mytestapp;


public class ExpenseSelection  extends Fragment {

    public static final ExpenseSelection newInstance() {
        ExpenseSelection f = new ExpenseSelection();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_expense_selection, container, false);

        return v;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);


        ViewGroup view = (ViewGroup)getView().findViewById(R.id.expensemainlayout);
        ExpenseIconGridGenerate.GenerateIcons(getActivity(), view);
    }
}

支持以下XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/expensemainlayout"
    tools:context=".ExpenseSelection" >

    <LinearLayout
        android:id="@+id/ExpenseIconsLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:height="10dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
    </LinearLayout>

</RelativeLayout>

在'getView'行上,我发现了异常。

我想要做的就是在oncreate方法中获取视图,我认为在构建表单控件时我应该做什么?否则我可以简单地使用onCreateView中的View来生成活动视图,但从设计的角度看它似乎是错误的。

以下是该行执行时的日志文件:

> 04-26 22:47:12.400: D/AndroidRuntime(4036): Shutting down VM 04-26
> 22:47:12.400: W/dalvikvm(4036): threadid=1: thread exiting with
> uncaught exception (group=0x41ebc930) 04-26 22:47:12.440:
> E/AndroidRuntime(4036): FATAL EXCEPTION: main 04-26 22:47:12.440:
> E/AndroidRuntime(4036): java.lang.NullPointerException 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> com.example.mytestapp.ExpenseSelection.onCreate(ExpenseSelection.java:34)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:801)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.support.v4.view.ViewPager.populate(ViewPager.java:1009) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.support.v4.view.ViewPager.populate(ViewPager.java:880) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.view.View.measure(View.java:15518) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.view.Choreographer.doCallbacks(Choreographer.java:562) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.Choreographer.doFrame(Choreographer.java:532) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> android.os.Handler.handleCallback(Handler.java:725) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.os.Handler.dispatchMessage(Handler.java:92) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> android.os.Looper.loop(Looper.java:137) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> android.app.ActivityThread.main(ActivityThread.java:5041) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> java.lang.reflect.Method.invokeNative(Native Method) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> java.lang.reflect.Method.invoke(Method.java:511) 04-26 22:47:12.440:
> E/AndroidRuntime(4036):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> 04-26 22:47:12.440: E/AndroidRuntime(4036):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-26
> 22:47:12.440: E/AndroidRuntime(4036):     at
> dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

不应该通过R.layout.expensemainlayout调用您的视图而不是R.id.expensemainlayout

答案 1 :(得分:1)

Luksprog所说的完全正确。检查here以查看Fragment生命周期。您可以尝试以下代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_expense_selection, container, false);

    ViewGroup myViewGroup = (ViewGroup)v.findViewById(R.id.expensemainlayout);
    ExpenseIconGridGenerate.GenerateIcons(getActivity(), myViewGroup);

    return v;
}

您也可以使用此方式删除onCreate当前的方式。