java - getReseources() 位图数组出现 NullPointerException

标签 java android android-layout bitmap nullpointerexception

我是 android 开发的新手,遇到了一个问题,我试图创建一个显示在画廊中的图像数组,当我点击图片时,它会在底部显示图片。当我运行该应用程序时,它崩溃了。我能得到的任何帮助都会非常非常有帮助。提前致谢。

我的问题是

  1. 如何摆脱 NullPointerException
  2. 我是否正确解码了图片?谁能告诉我更好的方法?

谢谢

我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".PicturesActivity" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/trophykiss" />

</RelativeLayout>

我的类(class):

public class PicturesActivity extends Activity {   

    Bitmap[] myImages = new Bitmap[] {
         BitmapFactory.decodeResource(getResources(), R.drawable.champions),
         BitmapFactory.decodeResource(getResources(), R.drawable.trophykiss),
         BitmapFactory.decodeResource(getResources(), R.drawable.championstwo),
         BitmapFactory.decodeResource(getResources(), R.drawable.trophies),
         BitmapFactory.decodeResource(getResources(), R.drawable.culture),
         BitmapFactory.decodeResource(getResources(), R.drawable.maintrophy),
         BitmapFactory.decodeResource(getResources(), R.drawable.dive),
         BitmapFactory.decodeResource(getResources(), R.drawable.naijamain),
         BitmapFactory.decodeResource(getResources(), R.drawable.ethiopia),
         BitmapFactory.decodeResource(getResources(), R.drawable.peru),
         BitmapFactory.decodeResource(getResources(), R.drawable.funtime),
         BitmapFactory.decodeResource(getResources(), R.drawable.skils),
         BitmapFactory.decodeResource(getResources(), R.drawable.gabon),
         BitmapFactory.decodeResource(getResources(), R.drawable.gambia),
         BitmapFactory.decodeResource(getResources(), R.drawable.guinea)
    };


    private ImageView imageView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pictures);

        Gallery g = (Gallery) findViewById(R.id.gallery1);
        g.setAdapter(new ImageAdapter(this));
        imageView = (ImageView) findViewById(R.id.image1);



                g.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "pic: " + position,
                        Toast.LENGTH_SHORT).show();
                imageView.setImageBitmap(myImages[position]);

            }



        });
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.MyGallery);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.MyGallery_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return myImages.length;
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);


            i.setImageBitmap(myImages[position]);
            i.setLayoutParams(new Gallery.LayoutParams(200, 200));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
    }
}

错误信息:

java.lang.NullPointerException: Attempt to invoke virtual method 
'android.content.res.Resources android.content.Context.getResources()

最佳答案

1) 在当前 Activity 中调用 onCreate() 之前,您无法访问 Context 对象。对于您当前使用它的方式,只需将数组的初始化移动到您的 onCreate() 方法中。

2) 由于您一次要解码这么多图像,因此这应该发生在后台线程上。看AsyncTask有关如何将图像加载到单独线程中的文档。

关于java - getReseources() 位图数组出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29677215/

相关文章:

android - ViewPagerIndicator : TabPagerIndicator not visible

android - 如何以适当的方式缩放 View/ViewGroup - 动 Canvas 局?

java - 为什么我的音频剪辑播放不一致?

android - 同步两个 GLSurfaceViews

android - 如何使用 ndk8 在 Windows 上为 Android 构建 Openssl?

android - 在 Fragment 更改时更改状态栏颜色 [Android Lollipop]

java - 为什么要在小对象上同步?

Java System.nanoTime() 与 System.currentTimeMillis()。为什么他们有不同的输出?

Java:使用数组,因此将简短的数字转换为字代码

android - 为什么 Android 使用 JUnit 而不是 TestNG,即使 Cedric Beust 创建了 TestNg 并且是 Android 团队的一员?