0

我有以下代码:

    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 中的视图来生成活动视图,但从设计的角度来看似乎是错误的。

这是该行执行时的日志文件:

> 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)
4

2 回答 2

1

Luksprog 所说的完全正确。在此处查看片段生命周期。您可以尝试以下代码:

 @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使用它删除当前的方式。

于 2013-04-26T12:56:24.837 回答
1

你不应该用 R.layout.expensemainlayout 而不是 R.id.expensemainlayout 来调用你的视图吗

于 2013-04-26T12:55:30.640 回答