在自定义对话框中显示Gallery的效果实现

来源:互联网 发布:labtool48uxp软件下载 编辑:程序博客网 时间:2024/06/05 05:50

  在做程序时,发现错误无知所措,当老师讲解时才大明所误。当出现错误时,静下心来,慢慢的重新理清一下思路,就会发现错误的所在。老是提示NullPointerException错误,并且在打开ImageButton按钮时无法弹出自定义对话框。原因在于获取gallery时获取错误,要获取的是Gallery中的,我却定义成了this在当前布局中获取,才无法找到gallery,出现空指针错误。

效果图:

 

布局设计:

activity_main.xml:

<LinearLayout 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:orientation="horizontal"    android:background="@drawable/image00" >    <ImageButton        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:layout_marginLeft="10dp"        android:src="@drawable/image01" />    <EditText        android:id="@+id/name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:layout_marginTop="20dp"        android:layout_marginLeft="5dp"        android:ems="10" >        <requestFocus />    </EditText>   </LinearLayout>


gallery.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:spacing="20dp"        android:layout_marginTop="20dp" /></RelativeLayout>


代码实现:

MainActivity.java:

import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewDebug;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.ImageButton;public class MainActivity extends Activity {private ImageButton imgbtn;private Gallery gallery;private int index=0;private ImageAdapter adapter;private int[] images = { R.drawable.image01, R.drawable.image02,R.drawable.image03, R.drawable.image04, R.drawable.image05,R.drawable.image06, R.drawable.image07, R.drawable.image08};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imgbtn=(ImageButton) this.findViewById(R.id.image);imgbtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {getDialog();}});}public void getDialog(){AlertDialog.Builder dialog=new AlertDialog.Builder(this);dialog.setTitle("请选择头像");LayoutInflater inflater=getLayoutInflater();View view=inflater.inflate(R.layout.galley, null);dialog.setView(view);gallery=(Gallery) view.findViewById(R.id.gallery);adapter=new ImageAdapter(this);gallery.setAdapter(adapter);gallery.setFocusable(true);gallery.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {// TODO Auto-generated method stubadapter.setImages(position);index=position;}});dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {imgbtn.setImageResource(images[index]);}});dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});dialog.create().show();}@Overridepublic 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;}}

ImageAdapter.java:

import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {private Context context;int mGalleryItemBackground;int img=0;private int[] images = { R.drawable.image01, R.drawable.image02,R.drawable.image03, R.drawable.image04, R.drawable.image05,R.drawable.image06, R.drawable.image07, R.drawable.image08};public ImageAdapter(Context context){this.context=context;TypedArray a=context.obtainStyledAttributes(R.styleable.HelloGallery);mGalleryItemBackground=a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground,0);    a.recycle();}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn images.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn images[position];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}public void setImages(int position){if(img!=position)img=position;notifyDataSetChanged();}@Overridepublic View getView(int position, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubImageView imageview=new ImageView(context);imageview.setImageResource(images[position]);if(img==position){imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));}else {imageview.setLayoutParams(new Gallery.LayoutParams(80, 80));}imageview.setBackgroundResource(mGalleryItemBackground);return imageview;}}