ImageButton组件的应用(更改头像)

前段时间就做的这个项目,一直没能运行成功,说是空指针异常。。今天突然看到,又小小研究了一番,终于运行成功~写篇博客和大家分享一下,希望大家不要犯和我一样的错误哦~

阶段一:进行主界面的布局

                                

具体代码如下:

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

    <ImageButton
        android:id="@+id/head"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/image0" />
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />
</LinearLayout>

阶段二:新建dialog.xml文件,实现对话框的布局,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:background="#55000000"
        android:spacing="10dp" />

</LinearLayout>

阶段三:编写Activity,查找组件并进行相应的事件处理(点击imageButton弹出更改头像对话框)。具体代码如下:

package com.lks.changeimage;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnItemClickListener{
	private ImageButton avatar;
	private Gallery gallery;
	private int[] images = { R.drawable.image1, R.drawable.image2,
			R.drawable.image3, R.drawable.image4,R.drawable.image5 };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		avatar = (ImageButton) this.findViewById(R.id.head);		
		avatar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				LayoutInflater inflater = LayoutInflater
						.from(MainActivity.this);
				final View dialogView = inflater.inflate(R.layout.dialog, null);
				AlertDialog.Builder builder = new AlertDialog.Builder(
						MainActivity.this);
				gallery = (Gallery) dialogView.findViewById(R.id.gallery);
				gallery.setAdapter(new ImageAdapter());
				gallery.setSelection(images.length / 2);
				gallery.setOnItemClickListener(MainActivity.this);
				builder.setIcon(android.R.drawable.ic_dialog_dialer)
						.setTitle("更改头像")
						.setView(dialogView)
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {

									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {

									}
								}).create().show();

			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
 
	/**
	 * 负责产生gallery中的图片
	 */
	private class ImageAdapter extends BaseAdapter {
		private int mGalleryItemBackground;
		public ImageAdapter(){
			TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery);
		    mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
		    typedArray.recycle();
		}

		// 返回图片的个数,比如你想得到图片的个数
		@Override
		public int getCount() {
			return images.length;
		}

		@Override
		public Object getItem(int position) {
			return images[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView = new ImageView(MainActivity.this);
			imageView.setImageResource(images[position]);
			imageView.setBackgroundResource(mGalleryItemBackground);
			return imageView;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int postion,long id) {
		avatar.setImageResource(images[postion]);

	}

}

关于空指针异常的错误就出现在查找gallery组件的时候:
gallery = (Gallery) dialogView.findViewById(R.id.gallery); //查找组件必须从当前对话框的布局中查找,否则就出现了空指针异常

阶段四:运行结果显示

                         

                         

对应的完整项目已上传到资源~
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值