在Horizo​​ntalScrollView中显示触摸的图像

时间:2013-05-17 06:56:52

标签: android image-gallery horizontalscrollview

我在android中实现类似app的图库,代码从sdcard上的特定文件夹中获取图片并在Horizo​​ntalScrollView中显示。我想显示用户在imageView clickListener或imageSwitcher中单击的每张图片,如果可能的话,我的水平视图,但我得到NullPointerException。

这是我的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)

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

上面代码中唯一的可能性是flScreen为空。实际上,您尝试从活动中获取该视图,但您使用线性布局中的findViewById。这将返回布局内的视图,而不是活动内部。

解决方案是从活动中获取视图,并通过setter方法将其作为参数传递给线性布局。

答案 1 :(得分:0)

那是因为你的陈述

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

返回null,即 flScreen = null

如果没有setContentView,则无法使用 findViewById ...

您必须以编程方式创建flScreen的实例....