0

私がこれまでやってきたことは、

public class GalleryView extends Activity {
public Context context=getApplicationContext();

public Map< Integer,Drawable> dayMap;
Bitmap icon1 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica1);
Drawable d1 =new BitmapDrawable(context.getResources(),icon1);

Bitmap icon2 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica2);
Drawable d2 =new BitmapDrawable(context.getResources(),icon2);

Bitmap icon3 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica3);
Drawable d3 =new BitmapDrawable(context.getResources(),icon3);

Bitmap icon4 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica4);
Drawable d4 =new BitmapDrawable(context.getResources(),icon4);

Bitmap icon5 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica5);
Drawable d5 =new BitmapDrawable(context.getResources(),icon5);

ImageView imageView;

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

    setContentView(R.layout.main_gallery);
    dayMap=new HashMap<Integer,Drawable>();
    dayMap.put(0,d1);
    dayMap.put(1,d2);
    dayMap.put(2,d3);
    dayMap.put(3,d4);
    dayMap.put(4,d5);

    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(context));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getBaseContext(), 
                    "You have selected picture " + (arg2+1) + " of Antartica", 
                    Toast.LENGTH_SHORT).show();
            imageView.setImageDrawable(dayMap.get(arg2));

        }

    });

}


public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {

        return dayMap.size();
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageDrawable(dayMap.get(arg0));
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(160,130));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

}

}

しかし、私はこのエラーが発生しています-

06-26 17:29:27.171: E/AndroidRuntime(853): FATAL EXCEPTION: main
06-26 17:29:27.171: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sai.samples.views/com.sai.samples.views.GalleryView}: java.lang.NullPointerException
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.os.Looper.loop(Looper.java:130)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.reflect.Method.invokeNative(Native Method)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.reflect.Method.invoke(Method.java:507)
06-26 17:29:27.171: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 17:29:27.171: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 17:29:27.171: E/AndroidRuntime(853):  at dalvik.system.NativeStart.main(Native Method)
06-26 17:29:27.171: E/AndroidRuntime(853): Caused by: java.lang.NullPointerException
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
06-26 17:29:27.171: E/AndroidRuntime(853):  at com.sai.samples.views.GalleryView.<init>(GalleryView.java:24)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.Class.newInstanceImpl(Native Method)
06-26 17:29:27.171: E/AndroidRuntime(853):  at java.lang.Class.newInstance(Class.java:1409)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-26 17:29:27.171: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-26 17:29:27.171: E/AndroidRuntime(853):  ... 11 more

どんな助けでも大歓迎です。

4

2 に答える 2

0

getApplicationContext()内部で呼び出す必要がありますonCreate()。アクティビティが作成される前に呼び出すのは有効ではないと思います。

于 2012-06-26T12:08:50.440 に答える
0

この行はNPEを引き起こします

ga.setAdapter(new ImageAdapter(context));

アクティビティの外で初期化したため、コンテキストはですnullonCreate()

実際、

これらはデッド コードです (アクティビティ メソッドの外部で、またはそれを参照せずにアプリケーション コンテキストを取得することはできません)。

public Context context=getApplicationContext();
Bitmap icon1 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica1);
Drawable d1 =new BitmapDrawable(context.getResources(),icon1);
Bitmap icon2 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica2);
Drawable d2 =new BitmapDrawable(context.getResources(),icon2);
Bitmap icon3 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica3);
Drawable d3 =new BitmapDrawable(context.getResources(),icon3);
Bitmap icon4 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica4);
Drawable d4 =new BitmapDrawable(context.getResources(),icon4);
Bitmap icon5 = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.antartica5);
Drawable d5 =new BitmapDrawable(context.getResources(),icon5);

onCreate() またはアクティビティの他のメソッドの外側では実行できません。

したがって、これらのコードをアクティビティの onCreate() メソッドに入れます。

お気に入り、

public Context context;
Bitmap icon1, icon2, icon3, icon4, icon5;
Drawable d1, d2, d3, d4, d5; 
public Map< Integer,Drawable> dayMap;
ImageView imageView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_gallery);
 context=getApplicationContext();
 icon1 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica1);
 d1 =new BitmapDrawable(context.getResources(),icon1);
 icon2 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica2);
 d2 =new BitmapDrawable(context.getResources(),icon2);
 icon3 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica3);
 d3 =new BitmapDrawable(context.getResources(),icon3);
 icon4 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica4);
 d4 =new BitmapDrawable(context.getResources(),icon4);
 icon5 = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.antartica5);
 d5 =new BitmapDrawable(context.getResources(),icon5);
 .
 .
 .
于 2012-06-26T12:14:51.077 に答える