0

请如果有人可以帮我解决这个问题。我找了几天这个解决方案,找不到任何东西。感谢各位开发者!

问题是行中的 NullPointerExceptionlistView.setAdapter(adapter);避免此问题的唯一方法是添加行setContentView(R.layaut.pestana0);,但这会使他们不再看到带有 viewpager 的页面。这是我的应用程序的代码:

主要活动

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 回答