我在数据库sqlite中获取字符串theme_name,然后使用主题名称在库中设置图像。但是我的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
谢谢
答案 0 :(得分:2)
你有这个ImageView delcared一个初始化的内部循环
for (String themeName : list_theme) {
//prepare image in layout gallery
ImageView img_shelf = new ImageView(this);
//this iamgeview is not added to gridview or to the layout
}
所以你有一个NullPointerException
@
img_shelf.setOnClickListener(new View.OnClickListener() { // outside for loop
coz
private ImageView img_shelf; // is declared not initialized
@Override
protected void onCreate(Bundle savedInstanceState) {
在for循环中声明和初始化的ImageView
也不会添加到gridview或activity中。
答案 1 :(得分:1)
您在类上定义了img_shelf
变量,然后在循环中再次重新定义它(因此它仅在循环范围内实例化):
for (String themeName : list_theme) {
//prepare image in layout gallery
ImageView img_shelf = new ImageView(this);
...
}
这不会按预期工作。
我不确定您是如何使用它的,因为您似乎创建了多个ImageView
,但随后将OnClickListener
仅附加到其中一个OnClickListener
。您需要将ImageView
的代码移动到循环中,或者在类上创建一个{{1}}的变量列表。