使用gridView和intent创建图库图像

时间:2013-12-12 12:59:47

标签: android gridview android-intent imageview

我正在创建一个图库图片android应用程序,用户可以从GridView中存在的图片集中选择任何图片,系统会将所选项目返回到第一个活动并显示图像

问题是,当我点击按钮转到第二个活动(ImageGalleryActivity)时,gridview不存在且图片集不存在,任何人都可以帮我解决这个问题???

MainActivity.java

package com.devleb.harrypottergallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

    ImageView imgBig, imgBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    imgBig = (ImageView)findViewById(R.id.imgv);
    imgBtn = (ImageView)findViewById(R.id.btn_img);
    imgBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent i = new Intent(this, ImageGalleryActivity.class);
        startActivity(i);
    }

}

ImageGalleryActivity.java

package com.devleb.harrypottergallery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageGalleryActivity extends Activity implements
        OnItemClickListener {

    GridView gridV;

    ImageView imgg;

    // array of small images
    public Integer[] smallImage = new Integer[] { R.drawable.smallpic01,
            R.drawable.smallpic02, R.drawable.smallpic03,
            R.drawable.smallpic04, R.drawable.smallpic05,
            R.drawable.smallpic06, R.drawable.smallpic07,
            R.drawable.smallpic08, R.drawable.smallpic09,
            R.drawable.smallpic10, R.drawable.smallpic11,
            R.drawable.smallpic12, R.drawable.smallpic13,
            R.drawable.smallpic14, R.drawable.smallpic15,
            R.drawable.smallpic16, R.drawable.smallpic17,
            R.drawable.smallpic18, R.drawable.smallpic19,
            R.drawable.smallpic20, R.drawable.smallpic21,
            R.drawable.smallpic22, R.drawable.smallpic23,
            R.drawable.smallpic24, R.drawable.smallpic25,
            R.drawable.smallpic26, R.drawable.smallpic27,
            R.drawable.smallpic28, R.drawable.smallpic29, R.drawable.smallpic30 };

    // array of large images
    Integer[] largImage = new Integer[] { R.drawable.pic01, R.drawable.pic02,
            R.drawable.pic03, R.drawable.pic04, R.drawable.pic05,
            R.drawable.pic06, R.drawable.pic07, R.drawable.pic08,
            R.drawable.pic09, R.drawable.pic10, R.drawable.pic11,
            R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,
            R.drawable.pic15, R.drawable.pic16, R.drawable.pic17,
            R.drawable.pic18, R.drawable.pic19, R.drawable.pic20,
            R.drawable.pic21, R.drawable.pic22, R.drawable.pic23,
            R.drawable.pic24, R.drawable.pic25, R.drawable.pic26,
            R.drawable.pic27, R.drawable.pic28, R.drawable.pic29,
            R.drawable.pic30 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);

        gridV = (GridView) findViewById(R.id.grid_v);
        gridV.setAdapter(new GalleryIMGAdapter(this));
        gridV.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.image_gallery, menu);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        ViewImage(arg2);

    }

    private void ViewImage(int arg2) {
        // TODO Auto-generated method stub

        setContentView(R.layout.activity_main);
        imgg = (ImageView) findViewById(R.id.imgv);
        imgg.setImageResource(largImage[arg2]);
    }

}

class GalleryIMGAdapter extends BaseAdapter {

    private Context mc;

    public GalleryIMGAdapter(Context c) {
        this.mc = c;
    }

    @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 img;

        if (convertview == null) {
            img = new ImageView(mc);
            img.setLayoutParams(new GridView.LayoutParams(200, 180));
            img.setScaleType(ImageView.ScaleType.CENTER_CROP);
            img.setPadding(8, 8, 8, 8);
        }
        return null;
    }

}

ImageGalleryActivity.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ImageGalleryActivity" >

    <GridView
        android:id="@+id/grid_v"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

</RelativeLayout>

log cat

12-12 13:16:48.745: E/AndroidRuntime(1959): FATAL EXCEPTION: main
12-12 13:16:48.745: E/AndroidRuntime(1959): java.lang.NullPointerException
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.AbsListView.obtainView(AbsListView.java:2163)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.GridView.onMeasure(GridView.java:1040)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.View.measure(View.java:15848)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.os.Handler.handleCallback(Handler.java:730)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.os.Looper.loop(Looper.java:137)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at java.lang.reflect.Method.invoke(Method.java:525)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-12 13:16:48.745: E/AndroidRuntime(1959):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

更改

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

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

同时返回

return img;

getView而不是null

同时在getView

中添加以下内容
  img.setImageResource(smallImage[position])

使GalleryIMGAdapter成为图库图片活动的内部类

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ImageGalleryActivity" >

    <GridView
        android:id="@+id/grid_v"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

</RelativeLayout>

然后你有

 setContentView(R.layout.activity_main);
 imgg = (ImageView) findViewById(R.id.imgv);

在同一活动中两次setContentView也是一个糟糕的设计。

编辑:

public class ImageGalleryActivity  extends Activity implements
        OnItemClickListener {

    GridView gridV;
    public Integer[] smallImage = new Integer[] {
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridV = (GridView) findViewById(R.id.grid_v);
        gridV.setAdapter(new GalleryIMGAdapter(this));
        gridV.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

    class GalleryIMGAdapter extends BaseAdapter {

        private Context mc;

        public GalleryIMGAdapter(Context c) {
            this.mc = c;
        }

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

        @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 img;
                img = new ImageView(mc);
                img.setLayoutParams(new GridView.LayoutParams(200, 180));
                img.setScaleType(ImageView.ScaleType.CENTER_CROP);
                img.setPadding(8, 8, 8, 8);
                img.setImageResource(smallImage[position]);

            return img;
        }
    }
}

对齐

enter image description here