我正在尝试从设备上的图片中选择一张图片。我有这个代码在以前的应用程序中工作但由于某种原因它只是不起作用。 当我尝试将所选图像设置为imageview时,我的onActivityResult会给出一个nullpointer异常,所以我猜Bitmap为null。我尝试了很多教程并使用了该代码,重写了所有内容,但每次出现同样的问题时。我已经尝试了多个设备,运行android 4.4和android 4.2.2(因此它不仅仅是一个kitkat问题)。
我的代码:
public class Iconize extends Activity{
public ImageView ivMain;
LinearLayout topscrollitems;
Bitmap bitmap;
int REQUEST_CODE=1;
FrameLayout fl ;
Context c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iconize);
c= getApplicationContext();
topscrollitems = (LinearLayout) this.findViewById(R.id.ll_topscrollview);
fl= (FrameLayout)this.findViewById(R.id.framelayoutholder);
//some stuff not related to this question
//....//
//
final Context c = getApplicationContext();
pickImageFromDevice();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("STOP");
}
public void pickImageFromDevice() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
ivMain.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e){
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
和stacktrace:
04-21 10:28:18.499: W/System.err(25951): java.lang.NullPointerException
04-21 10:28:18.519: W/System.err(25951): at com.sa.iconize.Iconize.onActivityResult(Iconize.java:114)
04-21 10:28:18.519: W/System.err(25951): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
04-21 10:28:18.519: W/System.err(25951): at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
04-21 10:28:18.519: W/System.err(25951): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
04-21 10:28:18.519: W/System.err(25951): at android.app.ActivityThread.access$1300(ActivityThread.java:135)
04-21 10:28:18.519: W/System.err(25951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-21 10:28:18.519: W/System.err(25951): at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 10:28:18.519: W/System.err(25951): at android.os.Looper.loop(Looper.java:136)
04-21 10:28:18.519: W/System.err(25951): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-21 10:28:18.519: W/System.err(25951): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 10:28:18.519: W/System.err(25951): at java.lang.reflect.Method.invoke(Method.java:515)
04-21 10:28:18.519: W/System.err(25951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-21 10:28:18.519: W/System.err(25951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-21 10:28:18.519: W/System.err(25951): at dalvik.system.NativeStart.main(Native Method)
发生错误的行是:
ivMain.setImageBitmap(bitmap);
我认为我遗漏了一些非常明显的东西,因为这不应该很难,而且我之前在以前的项目中工作过......
答案 0 :(得分:0)
您确定ivMain
是否已正确初始化?