空指针异常
package my.photo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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.ImageButton;
import android.widget.ImageView;
public class HelloGridViewActivity extends Activity {
private ImageButton img = null;
private Intent intent = new Intent();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
img = (ImageButton)findViewById(R.id.img);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//单击GridView的相应的Item时候讲img的图片换成相应的图片
switch(position){
case 0:{
img.setImageResource(R.drawable.sample_0);
intent.setClass(HelloGridViewActivity.this, other.class);
startActivity(intent);};break;
case 1:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_1);
startActivity(intent);};break;
case 2:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_2);
startActivity(intent);};break;
case 3:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_3);
startActivity(intent);};break;
case 4:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_4);
startActivity(intent);};break;
case 5:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_5);
startActivity(intent);};break;
case 6:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_6);
startActivity(intent);};break;
case 7:{
intent.setClass(HelloGridViewActivity.this, other.class);
img.setImageResource(R.drawable.sample_7);
startActivity(intent);};break;
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = { R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0,
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, R.drawable.sample_0, 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 };
}
}
我是想实现一个图片应用程序,点击当前Activity中GridView中的对应图片,会转到other.java中在other.java中的ImageButton中显示这张图片,报错是在gridview.setOnItemClickListener(new OnItemClickListener())方法中
但是我没发现是空指针啊 ,资源都有啊 ,
这是other.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"
android:orientation="vertical" >
<ImageButton
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
这是R.java文件
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package my.photo;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
public static final int sample_0=0x7f020001;
public static final int sample_1=0x7f020002;
public static final int sample_2=0x7f020003;
public static final int sample_3=0x7f020004;
public static final int sample_4=0x7f020005;
public static final int sample_5=0x7f020006;
public static final int sample_6=0x7f020007;
public static final int sample_7=0x7f020008;
}
public static final class id {
public static final int gridview=0x7f050000;
public static final int img=0x7f050001;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int other=0x7f030001;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
--------------------编程问答--------------------
main.xml文件
<?xml version="1.0" encoding="utf-8"?>--------------------编程问答-------------------- 当然是空指针啊
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
setContentView(R.layout.main)
这个调用的是main布局,而这个布局里面没有img
img = (ImageButton)findViewById(R.id.img);
这个img只有在other布局里面才有
楼主可以通过Intent的方式将要显示的图片资源,例如R.drawable.sample_0传到Other.class里面再做处理。
--------------------编程问答-------------------- main.xml中没有对象的控件,所以空指针。 --------------------编程问答--------------------
我试试啊 嘿嘿 谢啦 --------------------编程问答--------------------
大侠 R文件中已经有了img的声明啦,我应该不管在哪儿用img都应该是作用于这个ImageButton的啊 ,为什么不行呢 ,必须在布局里面有这个控件才行呢 --------------------编程问答-------------------- --------------------编程问答-------------------- 最起码给个日志 截图吧。 --------------------编程问答-------------------- 上个日志呗 --------------------编程问答-------------------- 楼主理解错了,img = (ImageButton)findViewById(R.id.img);你这样写是直接从main.xml文件里面找img,LayoutInflater factory = LayoutInflater.from(youActivity.this);
final View view = factory.inflate(R.layout.other,null);
img = (ImageButton)view.findViewById(R.id.img);你必须从other这个xml才能取出img.
补充:移动开发 , Android