如何将coverflow(动态视图)添加到main.xml

时间:2012-07-27 07:25:13

标签: android gallery coverflow

我在我的应用程序中尝试使用coverflow 我想在我的main.xml中使用具有特定尺寸的linearlayout中的coverflow。 我试过但是我得到了这个错误..

07-27 19:43:23.427: E/AndroidRuntime(1111): FATAL EXCEPTION: main
07-27 19:43:23.427: E/AndroidRuntime(1111): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android/com.android.Coverflow_displayerActivity}: java.lang.NullPointerException
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.os.Looper.loop(Looper.java:132)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.main(ActivityThread.java:4123)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at java.lang.reflect.Method.invoke(Method.java:491)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at dalvik.system.NativeStart.main(Native Method)
07-27 19:43:23.427: E/AndroidRuntime(1111): Caused by: java.lang.NullPointerException
07-27 19:43:23.427: E/AndroidRuntime(1111):     at com.android.Coverflow_displayerActivity.onCreate(Coverflow_displayerActivity.java:48)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.Activity.performCreate(Activity.java:4397)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
07-27 19:43:23.427: E/AndroidRuntime(1111):     ... 11 more

这是我试过的代码

package com.android;

import java.io.FileInputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

import com.android.CoverAdapterView.OnItemClickListener;

public class Coverflow_displayerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);

//      
//      CoverFlow coverFlow;
//      coverFlow= new CoverFlow(this);

        coverFlow.setAdapter(new ImageAdapter(this));

        ImageAdapter coverImageAdapter = new ImageAdapter(this);

        coverImageAdapter.createReflectedImages();

        coverFlow.setAdapter(coverImageAdapter);

        coverFlow.setSpacing(-15);
        coverFlow.setSelection(5, true);

        setContentView(R.layout.main);
//      setContentView(coverFlow);
        coverFlow.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(CoverAdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Log.v("this happens only in india", "ok i know this position"
                        + position);
                Toast.makeText(getApplicationContext(), "position " + position,
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Use this if you want to use XML layout file
    //  setContentView(R.layout.main);


    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private FileInputStream fis;

        private Integer[] mImageIds = { R.drawable.kasabian_kasabian,

        R.drawable.killers_day_and_age, R.drawable.garbage_bleed_like_me,
                R.drawable.death_cub_for_cutie_the_photo_album,
                R.drawable.kasabian_kasabian,
                R.drawable.massive_attack_collected, };

        private ImageView[] mImages;

        public ImageAdapter(Context c) {
            mContext = c;
            mImages = new ImageView[mImageIds.length];
        }

        public boolean createReflectedImages() {
            // The gap we want between the reflection and the original image
            final int reflectionGap = 4;

            int index = 0;
            for (final int imageId : mImageIds) {
                Bitmap originalImage = BitmapFactory.decodeResource(
                        getResources(), imageId);
                int width = originalImage.getWidth();
                int height = originalImage.getHeight();

                // This will not scale but will flip on the Y axis
                Matrix matrix = new Matrix();
                matrix.preScale(1, -1);

                // Create a Bitmap with the flip matrix applied to it.
                // We only want the bottom half of the image
                Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
                        height / 2, width, height / 2, matrix, false);

                // Create a new bitmap with same width but taller to fit
                // reflection
                Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                        (height + height / 2), Config.ARGB_8888);

                // Create a new Canvas with the bitmap that's big enough for
                // the image plus gap plus reflection
                Canvas canvas = new Canvas(bitmapWithReflection);
                // Draw in the original image
                canvas.drawBitmap(originalImage, 0, 0, null);
                // Draw in the gap
                Paint deafaultPaint = new Paint();
                canvas.drawRect(0, height, width, height + reflectionGap,
                        deafaultPaint);
                // Draw in the reflection
                canvas.drawBitmap(reflectionImage, 0, height + reflectionGap,
                        null);

                // Create a shader that is a linear gradient that covers the
                // reflection
                Paint paint = new Paint();
                LinearGradient shader = new LinearGradient(0,
                        originalImage.getHeight(), 0,
                        bitmapWithReflection.getHeight() + reflectionGap,
                        0x70ffffff, 0x00ffffff, TileMode.CLAMP);
                // Set the paint to use this shader (linear gradient)
                paint.setShader(shader);
                // Set the Transfer mode to be porter duff and destination in
                paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
                // Draw a rectangle using the paint with our linear gradient
                canvas.drawRect(0, height, width,
                        bitmapWithReflection.getHeight() + reflectionGap, paint);

                ImageView imageView = new ImageView(mContext);
                imageView.setImageBitmap(bitmapWithReflection);

                imageView.setLayoutParams(new CoverFlow.LayoutParams(120, 180));
                imageView.setScaleType(ScaleType.MATRIX);
                mImages[index++] = imageView;

            }
            return true;
        }

        public int getCount() {
            // Log.v("getCoutn(int count)", "" + mImageIds.length);
            return mImageIds.length;
        }

        public Object getItem(int position) {
            // Log.v("getItem(int position)", "" + position);
            return position;
        }

        public long getItemId(int position) {
            // Log.v("getItemID(int position)", "" + position);
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            // Use this code if you want to load from resources
            // ImageView i = new ImageView(mContext);
            // i.setImageResource(mImageIds[position]);
            // i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
            // i.setScaleType(ImageView.ScaleType.MATRIX);
            // return i;

            // Log.v("getview int position mImages", ""+position);

            return mImages[position];
        }

        /**
         * Returns the size (0.0f to 1.0f) of the views depending on the
         * 'offset' to the center.
         */
        public float getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
            Log.v("getScale float ", " " + focused + " " + offset);
            return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
        }

        // View v=getView(position, convertView, parent);

    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <LinearLayout
        android:id="@+id/add_here"
        android:layout_width="700dp"
        android:layout_height="700dp"
        android:orientation="vertical" 
        android:background="@android:color/darker_gray"
        >

     <com.android.CoverFlow
            android:id="@+id/coverflow"
            android:layout_width="700dp"
            android:layout_height="700dp" />
    </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:2)

首先需要设置活动视图,以便findViewById从该视图中获取ID并映射控件。所以写

在映射控件之前

setContentView(R.layout.main);,即

CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);

答案 1 :(得分:2)

似乎是onCreate()中的错误。在尝试分配coverFlow(setContentView(R.layout.main);)之前,应调用CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);。这就是为什么coverFlow是NULL

答案 2 :(得分:2)

我同意这里的其他答案。问题是甚至在调用setContentView()之前尝试初始化小部件。但我相信用户正在尝试遵循同样的事情,因为他无法区分从XML设置内容和在代码中创建视图并将其设置为内容。

在上面的例子中,如果你可以看到,作者试图在这里创建一个Coverflow视图代码,

    CoverFlow coverFlow;
    coverFlow= new CoverFlow(this);

然后他使用setContentView(coverFlow);

设置了此视图

但在我们的案例中,我们使用XML生成的资源,

第一件事是使用setContentView()将xml映射到我们的Activity。因为只有这时系统才会知道它必须从我们使用SetContentView()提供给它的XML中查找资源。如果不是,它将导致NullPointerException,因为andorid将找不到资源,因为我们已经指定了查找的位置。

因此,对于您的情况,请按照上述答案提供

setContentView(R.layout.main);

首先,然后使用参考表单初始化您的视图,例如,

CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);