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

2 に答える 2

1

Luksprog の言ったことは正確です。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これを使用して、現在の方法で削除することもできます。

于 2013-04-26T12:56:24.837 に答える
1

R.id.expensemainlayout の代わりに R.layout.expensemainlayout でビューを呼び出すべきではありません

于 2013-04-26T12:55:30.640 に答える