5黑马币
刚学android,很简单的一个例子,帮忙看下代码,myImage为什么得不到值?(图片存在,r.id.iv_images也没问题)
public class MainActivity extends ActionBarActivity {
private static ImageView iv_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
.................
}
...............
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
iv_image = (ImageView) rootView.findViewById(R.id.iv_image); //可以得到ImageView,不为空
Options opts = new Options();
opts.inSampleSize = 2;
Bitmap myImage = BitmapFactory.decodeResource(getResources(), //调试发现,myImage为空,得不到ImageView
R.id.iv_image, opts);
return rootView;
}
}
}
|
最佳答案
查看完整内容
通过分析楼主的源代码,显然是你的myImage获取错了
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.id.iv_image, opts);
通过这条源代码是无法获取myImage的,R.id.iv_image代表的是iv_image
所以你想要获取myImage应该将这句代码改为
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.iv_image, opts);
R.drawable.iv_image代表的是图片资源的id,所以你应该在res目 ...
|