gpt4 book ai didi

java - Android - 设置 ImageAdapter NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 09:00:02 26 4
gpt4 key购买 nike

我的安卓有问题。我想使用以下代码为图库设置 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>

堆栈跟踪是:

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
}
...

然后在你的 Activity 中这样调用:

 g.setAdapter(new ImageAdapter(this, images));

关于java - Android - 设置 ImageAdapter NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com