我正在Android中创建一个图库应用程序但我在运行时给了我一个零点异常。请帮我纠正一下。这是我的代码
我使用Gallery和ImageSwitcher进行XML设计
我在这里,当用户点击图库图像时,在ImageSwitcher上移动动画
public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{
private ImageSwitcher mswitcher;
private Integer[] mThimId={R.drawable.fba,R.drawable.fbb,R.drawable.fbc,R.drawable.fbd,R.drawable.fbe,R.drawable.fbf,R.drawable.fbg,R.drawable.fbh,R.drawable.fbi,R.drawable.fbj};
private Integer[] mImageId={R.drawable.fba,R.drawable.fbb,R.drawable.fbc,R.drawable.fbd,R.drawable.fbe,R.drawable.fbf,R.drawable.fbg,R.drawable.fbh,R.drawable.fbi,R.drawable.fbj};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mswitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
mswitcher.setFactory(this);
mswitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
mswitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
Gallery g=(Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(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 onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
mswitcher.setImageResource(mImageId[arg2]);
}
public class ImageAdapter extends BaseAdapter {
private Context mcontext;
public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
mcontext=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThimId.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mcontext);
i.setImageResource(mThimId[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return null;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
return null;
}
}
这是我的XML文件代码
<Gallery
android:id="@+id/gallery1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="36dp" />
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="159dp"
android:layout_marginLeft="63dp" >
</ImageSwitcher>
答案 0 :(得分:0)
你应该回来了#i;而不是来自getView和makeView方法的null。