我有一个GridView
,其中包含一些点击时全屏显示的图片。
但是当我点击图片时Target must not be null
的应用程序崩溃:(
我的活动:
public class ArenaActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arena);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
GridView gv = (GridView) findViewById(R.id.grid_view);
gv.setAdapter(new ArenaGridViewAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(ArenaActivity.this, ArenaViewActivity.class);
i.putExtra("position", position);
startActivity(i);
}
});
}
}
我的适配器:
public class ArenaGridViewAdapter extends BaseAdapter {
private Context context = null;
public ArenaGridViewAdapter(Context context) {
this.context = context;
}
public int[] arenaImgs = {
R.drawable.ic_arena_01,
R.drawable.ic_arena_02,
R.drawable.ic_arena_03,
R.drawable.ic_arena_04,
R.drawable.ic_arena_05,
R.drawable.ic_arena_06
};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
view.setScaleType(CENTER_CROP);
}
Picasso.with(this.context) //
.load(arenaImgs[position]) //
.placeholder(R.drawable.ic_placeholder) //
.error(R.drawable.ic_error) //
.fit() //
.into(view);
return view;
}
@Override
public int getCount() {
return arenaImgs.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
}
最后是全屏:
public class ArenaViewActivity extends ActionBarActivity {
public int[] arenaImgs = {
R.drawable.ic_arena_01,
R.drawable.ic_arena_02,
R.drawable.ic_arena_03,
R.drawable.ic_arena_04,
R.drawable.ic_arena_05,
R.drawable.ic_arena_06
};
private ShareActionProvider mShareActionProvider;
private int urlshare;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arena);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
int position = i.getExtras().getInt("position");
this.urlshare = position;
ImageView iv = (ImageView) findViewById(R.id.arena_full_view);
Picasso.with(getApplicationContext())
.load(arenaImgs[position])
.fit()
.centerCrop()
.into(iv);
}
}
错误日志表明目标null位于:Picasso.with(getApplicationContext())
我解决了这个问题。全屏布局出错。
更改为:setContentView(R.layout.arena_full_view);
谢谢大家。