我在數據庫sqlite中獲取字符串主題名稱,然後使用主題名稱來設置圖庫中的圖像。但是我有一些空指針異常的錯誤。你能告訴我爲什麼會發生?我該怎麼辦?謝謝Nullpointer例外與圖片庫
這是我的代碼
public class MybookActivity_Gallery extends Activity{
private BooksDB db;
private Context context;
private HashMap< String, ArrayList<String>> hm;
private ImageView imgOldSelected;
private int resID;
private ImageView img_shelf;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mybookactivity_gallery);
context = this;
db = new BooksDB(context);
ArrayList<String> list_bokID = new ArrayList<String>();
ArrayList<String> list_title = new ArrayList<String>();
ArrayList<String> list_theme = new ArrayList<String>();
hm = new HashMap<String, ArrayList<String>>();
//get Theme_name
hm = db.getBookTheme();
if(hm.isEmpty() == true){
System.out.println("not have data");
}else{
//prepare theme, title, book_id
list_theme = hm.get("theme");
list_title = hm.get("title");
list_bokID = hm.get("bokID");
}
LinearLayout gallery = (LinearLayout)findViewById(R.id.images_gallery);
//image fullsize
final ImageView imgFullSize = (ImageView)findViewById(R.id.full_size_image);
for (String themeName : list_theme) {
//prepare image in layout gallery
ImageView img_shelf = new ImageView(this);
img_shelf.setLayoutParams(new LinearLayout.LayoutParams(150, 150));
img_shelf.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
img_shelf.setPadding(8, 8, 8, 8);
//set image resource
resID = getResources().getIdentifier(themeName, "drawable", getPackageName());
img_shelf.setImageResource(resID);
}
// img_shelf set it in on click
img_shelf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView selectedImage = (ImageView) v;
Bitmap bitmap = ((BitmapDrawable) selectedImage.getDrawable()).getBitmap();
if(imgOldSelected != null){
imgOldSelected.setBackgroundColor(Color.TRANSPARENT);
}
selectedImage.setBackgroundColor(Color.LTGRAY);
}
});
gallery.addView(img_shelf);
}
}
這是我的logcat錯誤
02-21 16:12:02.421: E/AndroidRuntime(29116): FATAL EXCEPTION: main
02-21 16:12:02.421: E/AndroidRuntime(29116): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.splash.bookguk_project/category_viewbook_sub2_1.MybookActivity_Gallery}: java.lang.NullPointerException
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.os.Looper.loop(Looper.java:137)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.ActivityThread.main(ActivityThread.java:4921)
02-21 16:12:02.421: E/AndroidRuntime(29116): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 16:12:02.421: E/AndroidRuntime(29116): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 16:12:02.421: E/AndroidRuntime(29116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-21 16:12:02.421: E/AndroidRuntime(29116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-21 16:12:02.421: E/AndroidRuntime(29116): at dalvik.system.NativeStart.main(Native Method)
02-21 16:12:02.421: E/AndroidRuntime(29116): Caused by: java.lang.NullPointerException
02-21 16:12:02.421: E/AndroidRuntime(29116): at category_viewbook_sub2_1.MybookActivity_Gallery.onCreate(MybookActivity_Gallery.java:73)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.Activity.performCreate(Activity.java:5206)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-21 16:12:02.421: E/AndroidRuntime(29116): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
02-21 16:12:02.421: E/AndroidRuntime(29116): ... 11 more
謝謝
你的onCreate中的哪一行是73號? –
MybookActivity_Gallery.java:73 line .. – PSK
'img_shelf'可能爲空。 – Raghunandan