NullPointerException en getReseources () Bitmap Array

Soy bastante nuevo para el desarrollo de Android y se encontró con un problema en el que estoy tratando de crear una matriz de imágenes que muestra en una galería y cuando hago clic en una imagen, muestra la imagen en la parte inferior. Cuando ejecuto la aplicación, se bloquea. Cualquier ayuda que pueda obtener será muy, muy útil. Y gracias de antemano.

Mis preguntas son

  1. ¿Cómo puedo deshacerme de la NullPointerException ?
  2. Estoy descodificando las imágenes correctamente? ¿Puede alguien mostrarme una mejor manera?

Gracias

Mi diseño:

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

MI CLASE:

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

MENSAJE DE ERROR:

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

1) No puede acceder a su objeto Context antes de que onCreate () haya sido llamado en su actividad actual. Para la forma en que actualmente tiene que trabajar, basta con mover la inicialización de su matriz en su método onCreate ().

2) Ya que está decodificando tantas imágenes a la vez, esto debería suceder en un hilo de fondo. Busque en la documentación de AsyncTask cómo extraer la carga de la imagen en un hilo independiente.

  • NullPointerException en FirebaseMessagingService.zzo (fuente desconocida)
  • quitar y agregar NPE de vista, nido de abeja, android
  • NullPointerException en rotación durante el comando dispatchCreateOptionsMenu, la traza de pila no incluye ninguna función en mi aplicación
  • Posible motivo de "Error: no se pueden generar carpetas de vista java.lang.NullPointerException"
  • Android captura la foto putExtra (MediaStore.EXTRA_OUTPUT, Uri.fromFile (photoFile)); los datos son nulos
  • Android spinner es nulo
  • ParseSdkContent failed No se pudo inicializar la clase android.graphics.Typeface
  • AddTextChangedListener () en EditText en android ActionBar
  • Android nullpointerexception en ActionBarDrawerToggle.syncState ()
  • Inbuild de Android (ActionImageCapture) intento devuelve la intención nula. No puede entregar el resultado {who = null}
  • NullPointerException Advertencia sobre getView () dentro del método onActivityCreated / onStart / onViewCreated
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.