2013-05-17 26 views
0

我在android中實現了一個像app一樣的圖庫,代碼從sdcard上的特定文件夾獲取圖片並在Horizo​​ntalScrollView中顯示它。我想顯示用戶在imageView clickListener或imageSwitcher中點擊的每張圖片,如果可能的話,在我的水平視圖下方,但我得到NullPointerException。在Horizo​​ntalScrollView中顯示被感動的圖像

這裏我的XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<HorizontalScrollView 
    android:id="@+id/horizontalScrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <com.example.test.MyHorizontalLayout 
     android:id="@+id/mygallery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" /> 
</HorizontalScrollView> 

<ImageView 
    android:id="@+id/fScreen" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</ImageView> 

MyHorizo​​ntalLayout CALSS:

public class MyHorizontalLayout extends LinearLayout { 

Context myContext; 
ArrayList<String> itemList = new ArrayList<String>(); 
ImageView flScreen; 

public MyHorizontalLayout(Context context) { 
    super(context); 
    myContext = context; 
} 

public MyHorizontalLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    myContext = context; 
} 

public MyHorizontalLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    myContext = context; 
} 

void add(String path) { 
    int newIdx = itemList.size(); 
    itemList.add(path); 
    addView(getImageView(newIdx)); 
} 

ImageView getImageView(final int i) { 
    Bitmap bm = null; 
    if (i < itemList.size()) { 
     bm = decodeSampledBitmapFromUri(itemList.get(i), 100, 100); 
    } 

    ImageView imageView = new ImageView(myContext); 

    imageView.setLayoutParams(new LayoutParams(100, 100)); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setImageBitmap(bm); 
    flScreen = (ImageView) findViewById(R.id.fScreen); 
    imageView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      if (i < itemList.size()) { 
       Bitmap bmp = BitmapFactory.decodeFile(itemList.get(i)); 
       flScreen.setImageBitmap(bmp); 
      } 

      Toast.makeText(myContext, "Clicked - " + itemList.get(i), 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 

    return imageView; 
} 

public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, 
     int reqHeight) { 
    Bitmap bm = null; 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, 
      reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    bm = BitmapFactory.decodeFile(path, options); 

    return bm; 
} 

public int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 

    return inSampleSize; 
} 

} 

的logcat:

05-17 05:49:42.107: E/AndroidRuntime(24489): FATAL EXCEPTION: main 
    05-17 05:49:42.107: E/AndroidRuntime(24489): java.lang.NullPointerException 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at com.example.test.MyHorizontalLayout$1.onClick(MyHorizontalLayout.java:76) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at android.view.View.performClick(View.java:4202) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at android.view.View$PerformClick.run(View.java:17340) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at android.os.Handler.handleCallback(Handler.java:725) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at android.os.Handler.dispatchMessage(Handler.java:92) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at android.os.Looper.loop(Looper.java:137) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at android.app.ActivityThread.main(ActivityThread.java:5039) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at java.lang.reflect.Method.invokeNative(Native Method) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at java.lang.reflect.Method.invoke(Method.java:511) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    05-17 05:49:42.107: E/AndroidRuntime(24489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-17 05:49:42.107: E/AndroidRuntime(24489): at dalvik.system.NativeStart.main(Native Method) 

得到任何幫助。

回答

0

上面代碼中唯一的可能性是flScreen爲空。事實上,您嘗試從活動中獲取該視圖,但是您使用線性佈局中的findViewById。這將在佈局內返回一個視圖,而不是在活動內部。

解決方案是從活動中獲取視圖,並通過setter方法將其作爲參數傳遞給線性佈局。

+0

感謝您的答覆,你有可能在這個問題上提供一些代碼嗎? – Sozi

+0

這就是你的角色:) – Snicolas

0

那是因爲你的發言

flScreen = (ImageView) findViewById(R.id.fScreen); 

返回NULL,即flScreen = NULL

不能使用findViewById無需的setContentView ...

你必須創建實例flScreen以編程方式....

+0

感謝你的回覆,我會得到你指向我的東西,但是你能爲這個問題提供一段代碼嗎? – Sozi

相關問題