0

我尝试在我的应用程序中使用coverflow我想在main.xml 中具有特定尺寸的线性布局中使用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);

    }

}

主要的.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>
4

3 回答 3

2

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

setContentView(R.layout.main);在映射控件之前,即

CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);
于 2012-07-27T07:30:01.030 回答
2

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

于 2012-07-27T07:30:12.497 回答
2

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

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

    CoverFlow coverFlow;
    coverFlow= new CoverFlow(this);

然后他设置了这个视图使用,setContentView(coverFlow);

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

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

因此,对于您的情况,只需遵循上述答案并提供

setContentView(R.layout.main);

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

CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);
于 2012-07-27T07:37:57.343 回答