0

誰かがこれを解決するのを手伝ってくれるならお願いします。この解決策を数日間探しましたが、何も見つかりませんでした。仲間の開発者に感謝します!

問題は NullPointerException 行ですlistView.setAdapter(adapter);。この問題を回避する唯一の方法は、行を追加することですsetContentView(R.layaut.pestana0);が、これにより、ビューページャーでページが表示されなくなります。ここに私のアプリケーションのコード:

主な活動

public class AwesomePagerActivity extends Activity {

String[] countries = new String[] { "India", "Pakistan", "Sri Lanka",
        "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea",
        "South Korea", "Japan" };

// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher };

// Array of strings to store currencies
String[] currency = new String[] { "Indian Rupee", "Pakistani Rupee",
        "Sri Lankan Rupee", "Renminbi", "Bangladeshi Taka",
        "Nepalese Rupee", "Afghani", "North Korean Won",
        "South Korean Won", "Japanese Yen" };

private ViewPager columnas;
private static int NUM_COLUMNAS = 3;
private ColumnasAdapter miAdapter;

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

    miAdapter = new ColumnasAdapter();
    columnas = (ViewPager) findViewById(R.id.columnas);
    columnas.setAdapter(miAdapter);

    // Parte nueva: Añadimos el Adapter al indicador
    TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titulos);
    titleIndicator.setViewPager(columnas);

}

private class ColumnasAdapter extends PagerAdapter {

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

    @Override
    public Object instantiateItem(View collection, int position) {
        final LayoutInflater inflater = (LayoutInflater) AwesomePagerActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v;
        switch (position) {
        case 0: {
            // setContentView(R.layout.pestana0);
            v = inflater.inflate(R.layout.pestana0, null, false);

            /*
             * Codigo De Cada Pestaña
             */

            // Each row in the list stores country name, currency and flag
            List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

            for (int i = 0; i < 10; i++) {
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("txt", "Country : " + countries[i]);
                hm.put("cur", "Currency : " + currency[i]);
                hm.put("flag", Integer.toString(flags[i]));
                aList.add(hm);
            }

            // Keys used in Hashmap
            String[] from = { "flag", "txt", "cur" };

            // Ids of views in listview_layout
            int[] to = { R.id.flag, R.id.txt, R.id.cur };

            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                    aList, R.layout.listview_layout, from, to);

            // Getting a reference to listview of main.xml layout file
            ListView listView = (ListView) findViewById(R.id.listview);

            // Setting the adapter to the listView
            listView.setAdapter(adapter);

        }
        case 1: // Full ExpandableList code here
        {
            v = inflater.inflate(R.layout.pestana1, null, false);

            /*
             * Codigo De Cada Pestaña
             */

            ((ViewPager) collection).addView(v, 0);
            return v;
        }
        case 2: // Full ExpandableList code here
        {
            v = inflater.inflate(R.layout.pestana2, null, false);

            /*
             * Codigo De Cada Pestaña
             */

            ((ViewPager) collection).addView(v, 0);
            return v;
        }
        default:
            v = inflater.inflate(R.layout.pestana0, null, false);

            /*
             * Codigo De Cada Pestaña
             */

            ((ViewPager) collection).addView(v, 0);
            return v;

        }
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((LinearLayout) view);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0)
            return "Autos";

        else {
            if (position == 1) {
                return "Planes";
            } else {

                if (position == 2) {
                    return "Contactenos";
                }
            }
        }
        return "Autos";

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public void finishUpdate(View arg0) {
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

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

    @Override
    public void startUpdate(View arg0) {
    }
}

}

xml ペスターナ0

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

listview_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView  android:id="@+id/flag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"/>

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">    

            <TextView 
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"/>           

            <TextView 
                android:id="@+id/cur"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10dp"/>
        </LinearLayout>
</LinearLayout>
4

1 に答える 1

2

次のように呼び出すだけです。

ListView listView = (ListView) v.findViewById(R.id.listview);
//                     add this ^

探している ListView は、現在表示されているレイアウトではなく、膨張させたばかりのレイアウトにあります。findViewById()したがって、 を参照するように指示する必要がありますv

于 2012-10-18T20:10:32.267 に答える