在做程序时,发现错误无知所措,当老师讲解时才大明所误。当出现错误时,静下心来,慢慢的重新理清一下思路,就会发现错误的所在。老是提示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};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgbtn=(ImageButton) this.findViewById(R.id.image);
imgbtn.setOnClickListener(new OnClickListener() {
@Override
public 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() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
adapter.setImages(position);
index=position;
}
});
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
imgbtn.setImageResource(images[index]);
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.create().show();
}
@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;
}
}
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();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void setImages(int position){
if(img!=position)
img=position;
notifyDataSetChanged();
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView 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;
}
}