0

以前は、viewPager の一部としてフラグメントで複製しようとしているアクティビティ レイアウトがありました。競合が発生した場合に備えて、古い XML ファイルをコピーし、すべての ID を変更しました。このフラグメントの新しい XML レイアウトと作成された新しい ID を参照しています。しかし、実行するとこの nullpointerexception が発生します。

ここにlogcatファイルがあります http://i48.tinypic.com/14ekq6s.png

エラーが発生する Java ファイルの部分を次に示します。

@Override
    public RelativeLayout onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        inflater.inflate(R.layout.one, null);
        ViewPager viewFinder = (ViewPager) getView();
//setContentView(R.layout.one);


        title = (TextView)     viewFinder.findViewById(R.id.frag_AA);  //ERROR xml id file probz

そして、これがレイアウトファイルです。

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


    <TextView
        android:id="@+id/frag_AA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="I Am Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
4

2 に答える 2

1

原因は、viewFinderが null であることです。

私はあなたが膨らませているのを見ますR.layout.one:

inflater.inflate(R.layout.one, null);

ただし、インフレータによって返されたビューは使用しません。 ビューを返します。ここでTextViewinflater.inflate()を探す必要があります。title

このようなもの:

View view = inflater.inflate(R.layout.one, null);
title = (TextView)view.findViewById(R.id.frag_AA);

(私は ViewPagers を使ったことはありませんが、このページに目を通すと、正しくないかもしれませんが、間違った使い方をしているように感じます)

于 2013-01-06T21:26:08.287 に答える
0

以下のコードを試してください..

View myview = inflater.inflate(R.layout.one, null);
title = (TextView)myview.findViewById(R.id.frag_AA);

インフレータがレイアウトを膨らませないnullため、

inflater.inflate(R.layout.one, null);リターン型です。リターンビューです。それがこの問題が発生する方法です。

于 2013-01-07T05:25:12.183 に答える