到目前为止,我所做的是
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
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
getApplicationContext()
内调用 onCreate()
。我不认为在创建活动之前调用它是有效的。
答案 1 :(得分:0)
此行导致 NPE
ga.setAdapter(new ImageAdapter(context));
因为您的上下文是null
,所以当您在活动的onCreate()
实际上,
这些是您的死代码,(您无法在活动方法的外部获取应用程序上下文或不参考它)
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()或任何其他Activity方法的外部执行此操作。
所以把这些代码放在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);
.
.
.