尝试使用Gallery应用程序显示空白屏幕

时间:2012-09-06 05:46:34

标签: android

我正在尝试一个图库应用程序,我找到了一个样本@ http://raivoratsep.com/114/android-gallery-tutorial-working-example/,我可以顺利构建,但在模拟器中它显示一个空白的白色屏幕...... 在这里我发布我的代码..     公共类GallaryActivity扩展了Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallary);
    Gallery g = (Gallery) findViewById(R.id.gallery1);
    final Gallary_adapter ga = new Gallary_adapter(this);
    g.setAdapter(ga);

    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(GallaryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
/*            Intent intent= new Intent("hello");
            long id1 = ga.getItemId(position);

          intent.putExtra("key", id1);

            sendBroadcast(intent);    */
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_gallary, menu);
    return true;
}
}

和Gallary_adapter类是这样的..

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

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };
Gallary_adapter(Context c){
    mContext=c;
    TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
    mGalleryItemBackground = a.getResourceId(
            R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView i = new ImageView(mContext);

    i.setImageResource(mImageIds[position]);
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);

    return i;
}



}

这个有用的链接我发现用'R.stylable'https://groups.google.com/forum/?fromgroups=#!topic/android-beginners/AJfST9YGADM

删除所有错误

这是xml布局文件..

<RelativeLayout 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:id="@+id/lay">

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="164dp" />

请帮帮我.. 谢谢你:)

2 个答案:

答案 0 :(得分:0)

你的getCount在你的Gallary_adapter类中返回零,

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

必须返回,

mImageIds这样的长度,

 @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mImageIds.length;
    }

GetCount负责计算图库中的项目数。

答案 1 :(得分:0)

你也可以使用ACTION_PICK意图

来做到这一点
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, 1);