画廊不工作

时间:2014-04-10 16:29:58

标签: android eclipse imageview gallery

我试图在我的应用程序中创建一个库,由于某种原因它给了我很多错误。它给我一个错误:

无法从视图投射到图库

 Gallery ga = (Gallery)findViewById(R.id.Gallery01);

方法setAdapter(Gallery.ImageAdapter)未定义类型Gallery

 ga.setAdapter(new ImageAdapter(this));

方法setOnItemClickListener(new AdapterView.OnItemClickListener(){})未定义类型Gallery

 ga.setOnItemClickListener(new OnItemClickListener()

Gallery1无法解析或不是字段

 TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);

Gallery1_android_galleryItemBackground无法解析或不是字段

 imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);

Gallery.LayoutParams无法解析为类型

iv.setLayoutParams(new Gallery.LayoutParams(150,120));

logcat的

[2014-04-10 12:15:44 - LoyaltyApp] Found 2 versions of android-support-v4.jar in the dependency list,
[2014-04-10 12:15:44 - LoyaltyApp] but not all the versions are identical (check is based on SHA-1 only at this time).
[2014-04-10 12:15:44 - LoyaltyApp] All versions of the libraries must be the same at this time.
[2014-04-10 12:15:44 - LoyaltyApp] Versions found are:
[2014-04-10 12:15:44 - LoyaltyApp] Path: /Users/alaaqamhieh/Desktop/LoyaltyApp/libs/android-support-v4.jar
[2014-04-10 12:15:44 - LoyaltyApp]  Length: 627582
[2014-04-10 12:15:44 - LoyaltyApp]  SHA-1: db0f122c99ef9f90dbab3fada6d191f2880cbb8e
[2014-04-10 12:15:44 - LoyaltyApp] Path: /Users/alaaqamhieh/Documents/workspace/appcompat_v7/libs/android-support-v4.jar
[2014-04-10 12:15:44 - LoyaltyApp]  Length: 648327
[2014-04-10 12:15:44 - LoyaltyApp]  SHA-1: ded9acc6a9792b8f1afc470f0c9cd36d178914cd
[2014-04-10 12:15:44 - LoyaltyApp] Jar mismatch! Fix your dependencies
[2014-04-10 12:15:45 - LoyaltyApp] Found 2 versions of android-support-v4.jar in the dependency list,
[2014-04-10 12:15:45 - LoyaltyApp] but not all the versions are identical (check is based on SHA-1 only at this time).
[2014-04-10 12:15:45 - LoyaltyApp] All versions of the libraries must be the same at this time.
[2014-04-10 12:15:45 - LoyaltyApp] Versions found are:
[2014-04-10 12:15:45 - LoyaltyApp] Path: /Users/alaaqamhieh/Desktop/LoyaltyApp/libs/android-support-v4.jar
[2014-04-10 12:15:45 - LoyaltyApp]  Length: 627582
[2014-04-10 12:15:45 - LoyaltyApp]  SHA-1: db0f122c99ef9f90dbab3fada6d191f2880cbb8e
[2014-04-10 12:15:45 - LoyaltyApp] Path: /Users/alaaqamhieh/Documents/workspace/appcompat_v7/libs/android-support-v4.jar
[2014-04-10 12:15:45 - LoyaltyApp]  Length: 648327
[2014-04-10 12:15:45 - LoyaltyApp]  SHA-1: ded9acc6a9792b8f1afc470f0c9cd36d178914cd
[2014-04-10 12:15:45 - LoyaltyApp] Jar mismatch! Fix your dependencies
[2014-04-10 12:15:51 - LoyaltyApp] Found 2 versions of android-support-v4.jar in the dependency list,
[2014-04-10 12:15:51 - LoyaltyApp] but not all the versions are identical (check is based on SHA-1 only at this time).
[2014-04-10 12:15:51 - LoyaltyApp] All versions of the libraries must be the same at this time.
[2014-04-10 12:15:51 - LoyaltyApp] Versions found are:
[2014-04-10 12:15:51 - LoyaltyApp] Path: /Users/alaaqamhieh/Desktop/LoyaltyApp/libs/android-support-v4.jar
[2014-04-10 12:15:51 - LoyaltyApp]  Length: 627582
[2014-04-10 12:15:51 - LoyaltyApp]  SHA-1: db0f122c99ef9f90dbab3fada6d191f2880cbb8e
[2014-04-10 12:15:51 - LoyaltyApp] Path: /Users/alaaqamhieh/Documents/workspace/appcompat_v7/libs/android-support-v4.jar
[2014-04-10 12:15:51 - LoyaltyApp]  Length: 648327
[2014-04-10 12:15:51 - LoyaltyApp]  SHA-1: ded9acc6a9792b8f1afc470f0c9cd36d178914cd
[2014-04-10 12:15:51 - LoyaltyApp] Jar mismatch! Fix your dependencies

Activity_Gallery.java

import com.example.loyaltyapp.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView ;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Gallery extends Activity {

 Integer[] pics = {
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
            R.drawable.back,
    };

 ImageView imageView;

 /** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(this));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getBaseContext(), 
                    "You have selected picture " + (arg2+1) + " of Antartica", 
                    Toast.LENGTH_SHORT).show();
            imageView.setImageResource(pics[arg2]);

        }

    });

}

 public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int imageBackground;

        public ImageAdapter(Context c) {
            ctx = c;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
        }

        @Override
        public int getCount() {

            return pics.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            iv.setImageResource(pics[arg0]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(150,120));
            iv.setBackgroundResource(imageBackground);
            return iv;
        }

    }

activity_gallery.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" >

<Gallery
    android:id="@+id/Gallery01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</Gallery>

 <ImageView
    android:id="@+id/ImageView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
 </ImageView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的代码显示错误,因为您的活动也被命名为Gallery。更改活动的名称。而不是android.widget.Gallery的对象,而是创建活动的对象