0

実行時にnullポインタ例外が発生します。PagerViewの2つのXMLファイルからRelativeLayoutsを取得していますが、これはめちゃくちゃになっているようです。エラーの原因は何ですか?

助けてくれてありがとう!

同じ構造を持つこれらのXMLファイルが2つあります--player_turn_solution_component.xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/player_turn_solution_component"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/solutionCaption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="what do you see?" />

        <EditText
            android:id="@+id/solutionInput"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/solutionCaption"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </RelativeLayout>

私のインスタンス化機能:

@Override
    public Object instantiateItem(View collection, int position) {
        //Our specific component (TextView or ImageView, depending on posision
        //is placed inside a RelativeLayout that acts as a container
        RelativeLayout container = new RelativeLayout(pagerContext);
        TextView caption = new TextView(pagerContext);
        caption.setText("View: " + position);
        caption.setTypeface(typeface);
        caption.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        container.addView(caption);

        RelativeLayout optionLayout = null;
        TextView optionCaption = null;

        if (position == 0) {
            optionLayout = (RelativeLayout) findViewById(R.id.player_turn_solution_component);
            optionCaption = (TextView) findViewById(R.id.solutionCaption);
        } else if (position == 1) {
            optionLayout = (RelativeLayout) findViewById(R.id.player_turn_clue_component);
            optionCaption = (TextView) findViewById(R.id.clueCaption);
        }

        container.addView(optionLayout);

        ((ViewPager) collection).addView(container,0);  //get the container ready
        return container;   
    }

そして、LogCat出力:

07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewGroup.addView(ViewGroup.java:3158)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewGroup.addView(ViewGroup.java:3145)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at my.app.PlayerTurnActivity$HelpPagerAdapter.instantiateItem(PlayerTurnActivity.java:136)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1375)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.View.measure(View.java:12892)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.os.Looper.loop(Looper.java:137)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at android.app.ActivityThread.main(ActivityThread.java:4512)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at java.lang.reflect.Method.invoke(Method.java:511)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
07-06 09:30:22.337: E/AndroidRuntime(8418):     at dalvik.system.NativeStart.main(Native Method)

編集:私の新しいアクティビティには次のものが含まれていますが、同じNPEでクラッシュしますが、スワイプしてページャービューを変更した場合のみです。

if (position == 0) {
        View v = getLayoutInflater().inflate(R.layout.player_turn_solution_component, null);                
        optionLayout = (RelativeLayout) v.findViewById(R.id.player_turn_solution_component);
    } else if (position == 1) {
        View v = getLayoutInflater().inflate(R.layout.player_turn_clue_component, null);    
        optionLayout = (RelativeLayout) v.findViewById(R.id.player_turn_clue_component);
    }

    container.addView(optionLayout);

    ((ViewPager) collection).addView(container,0);  //get the container ready
    return container;   
4

1 に答える 1

1

あなたはこのようなものが必要です

View v = getLayoutInflater().inflate(R.layout.layout_taht_have_player_turn_solution_component, null);

 if (position == 0) {
            optionLayout = (RelativeLayout) v.findViewById(R.id.player_turn_solution_component);
            optionCaption = (TextView) v.findViewById(R.id.solutionCaption);
        } 
于 2012-07-06T13:42:07.917 に答える