2014-02-21 54 views
0

我在數據庫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 

謝謝

+0

你的onCreate中的哪一行是73號? –

+0

MybookActivity_Gallery.java:73 line .. – PSK

+0

'img_shelf'可能爲空。 – Raghunandan

回答

1

您在類中定義img_shelf變量,然後你在循環中重新定義它(所以它僅在內部實例化)環的範圍):

for (String themeName : list_theme) { 
    //prepare image in layout gallery 
    ImageView img_shelf = new ImageView(this); 
    ... 
} 

這不會按預期工作。

我不確定你想如何使用它,因爲看起來你創建了多個ImageView's但是隻附加OnClickListener其中的一個。您需要將OnClickListener的代碼移動到循環中,或者在課程上創建一個變量 - 列表ImageView

2

您有循環

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 
    } 

這個ImageView的delcared一個初始化內讓您擁有一個NullPointerException @

img_shelf.setOnClickListener(new View.OnClickListener() { // outside for loop 

堂妹

private ImageView img_shelf; // is declared not initialized 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

還宣佈和初始化在ImageView循環不會添加到gridview或activ性。

+0

謝謝拉古南丹 – user3001046

+0

@ user3001046通過點擊旁邊的勾號接受答案 – Raghunandan