0

我在android中实现了一个类似应用程序的画廊,代码从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 类:

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;
}

}

日志猫:

    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)

感谢任何帮助。

4

2 回答 2

0

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

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

于 2013-05-17T07:01:50.247 回答
0

那是因为你的陈述

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

返回 null,即flScreen = null

没有 setContentView 就不能使用findViewById ...

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

于 2013-05-17T07:02:11.543 回答