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 を使用します。これは、アクティビティ内ではなく、レイアウト内のビューを返します。

解決策は、アクティビティからビューを取得し、セッター メソッドを介してパラメーターとしてリニア レイアウトに渡すことです。

于 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 に答える