1

こんにちは、3 ページを含むスワイプ ページの例があり、この例にリスト ビューを追加したいと考えています。
しかし、そのEclipseを実行しようとすると、「コンテンツには、id属性が「android.R.id.list」であるListViewが必要です」と表示されます。何が悪いのかわからない。
私のメイン アクティビティ レイアウト

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

    <android.support.v4.view.ViewPager
        android:id="@+id/customviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />    

     </LinearLayout>    

ページ1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
android:orientation="vertical" >     

<Button
    android:id="@+id/buttonBlue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_blue"
    android:onClick="@string/listenerBlueButton" />

 </LinearLayout>     

ページ2

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


   android:layout_height="match_parent"
    android:background="@color/yellow"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonYellow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/label_yellow"
        android:onClick="@string/listenerYellowButton" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    </LinearLayout>  

3ページ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical" >

<Button
    android:id="@+id/buttonRed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_red"
    android:onClick="@string/listenerRedButton" />

</LinearLayout>

マイページャーアダプター

   package ro.ovidiuconeac.horizontalviewswiping;

     public class CustomPagerAdapter extends PagerAdapter {

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0: {
            resId = R.layout.page_1;
            break;
        }
        case 1: {
            resId = R.layout.page_2;
            break;
        }
        case 2: {
            resId = R.layout.page_3;
            break;
        }
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
    }

主な活動

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create and set adapter
    CustomPagerAdapter adapter = new CustomPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);

    String[] ex=new String[10];
    ex[0]="1";
    ex[1]="2";
    ex[2]="3";
    ex[3]="1";
    ex[4]="2";
    ex[5]="3";
    ex[6]="1";
    ex[7]="2";
    ex[8]="3";
    ex[9]="3";

    setContentView(R.layout.page_2);
    ListView myList=(ListView)findViewById(android.R.id.list);
    ListAdapter la=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ex);

    myList.setAdapter(la);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

/**
 * Click button on blue page
 */
public void onClickBlueButton(View v) {
    Toast.makeText(getApplicationContext(), "Blue screen", Toast.LENGTH_SHORT).show();
}

/**
 * Click button on yellow page
 */
public void onClickYellowButton(View v) {
    Toast.makeText(getApplicationContext(), "Yellow screen", Toast.LENGTH_SHORT).show();
}

/**
 * Click button on red page
 */
public void onClickRedButton(View v) {
    Toast.makeText(getApplicationContext(), "Red screen", Toast.LENGTH_SHORT).show();
}
}
4

2 に答える 2

1

これは、ListActivity を拡張しているためです。ListActivity は、Android パッケージから特定の識別子を持つ ListView を探します。

ListActivity のこの行を見てください

setContentView(R.layout.activity_main);

ListActivity は、ページ 2 (リストビューを含む) のレイアウトではなく、メイン アクティビティのレイアウトを膨らませています。

ListActivity の onCreate でその行を変更して、正しいレイアウトに膨らませる必要があります。ListActivity レイアウトが R.layout.list_activy と呼ばれるとしましょう。その行は次のようになります

setContentView(R.layout.list_activity);

あなたが現在持っているものの代わりに、リストビューを含むレイアウトファイルの名前が何と呼ばれているのかわかりません。

編集: とにかく setAdapter() で親レイアウトをアダプターに渡すため、 ListActivity は setContentView() の呼び出しをまったく必要としない場合があることを思い出しました。その行を完全に削除してみて、setAdapter を呼び出してください。ただし、これらのアプローチは両方とも機能するはずです

于 2013-06-26T01:04:53.360 に答える
0

XML に含まれていますListViewが、アダプターを XML に渡していません。を作成しListView、アダプタをこのリスト ビューに渡します。

これを試して

ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(la);
于 2013-06-25T19:44:33.297 に答える