我对安卓系统有问题。
我要使用以下代码为库设置ImageAdapter:

Integer[] images = {R.drawable.image01};
static int counter = 0;

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

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

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

    Gallery g = (Gallery) findViewById(R.id.gallery1);
    g.setAdapter(new ImageAdapter(this, images));
}

我想使用imageadapter将图像“image01”添加到库中。
但这段代码会导致nullpointerexception。ImageAdapter的代码是:
public class ImageAdapter extends BaseAdapter {
    Context m;
    public ImageAdapter(Context c){
        m = c;
    }

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

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(m);
        i.setImageResource(images[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 150));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        return i;

    }
}

编辑:我的XML是:
<?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:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="17.92"
        android:spacing="@dimen/activity_horizontal_margin" />

</LinearLayout>

stacktrace是:
04-05 22:11:38.144: D/AndroidRuntime(32124): Shutting down VM
04-05 22:11:38.144: W/dalvikvm(32124): threadid=1: thread exiting with uncaught exception (group=0x40ebf930)
04-05 22:11:38.144: E/AndroidRuntime(32124): FATAL EXCEPTION: main
04-05 22:11:38.144: E/AndroidRuntime(32124): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.bw2801.uwelugemediathek/com.bw2801.uwelugemediathek.MainActivity}:   java.lang.NullPointerException
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.os.Looper.loop(Looper.java:137)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at java.lang.reflect.Method.invoke(Method.java:511)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at dalvik.system.NativeStart.main(Native   Method)
04-05 22:11:38.144: E/AndroidRuntime(32124): Caused by: java.lang.NullPointerException
04-05 22:11:38.144: E/AndroidRuntime(32124):    at com.bw2801.uwelugemediathek.MainActivity.onCreate(MainActivity.java:79)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.Activity.performCreate(Activity.java:5104)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-05 22:11:38.144: E/AndroidRuntime(32124):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-05 22:11:38.144: E/AndroidRuntime(32124):    ... 11 more

最佳答案

你的图像对象不存在于你的适配器中,所以
向适配器的构造函数中添加新参数以接受图像

public class ImageAdapter extends BaseAdapter {
    Context m;
    Integer[] images
    public ImageAdapter(Context c, Integer[] images ){
        m = c;
        this.images = images
    }
...

然后在你的活动中像这样打电话:
 g.setAdapter(new ImageAdapter(this, images));

08-06 11:47