我想从移动存储中将图像加载到应用程序的图像视图中。可以肯定的是,我保留了内部和外部存储的形象。但是在编译时,我得到一个空指针异常。我也在可绘制的文件夹中放置了相同的图像,并尝试从那里设置图像视图。但面临同样的问题。有人能帮我一下吗!。我正在添加我尝试过的代码片段。在ImageView中设置位图时出现NullPointer异常
方法-1:
File imageFile = new File(Environment.getExternalStorageDirectory(),"golden_eagle.jpg");
if(imageFile.exists()) {
Log.v(LOGTAG, "Image file exists");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap outputImageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
imageView.setImageBitmap(outputImageBitmap);
}
方法-2:
Bitmap bitimage;
bitimage = BitmapFactory.decodeResource(getResources(), R.drawable.golden_eagle);
if(bitimage == null)
Log.v(LOGTAG,"bitimage is empty");
else
Log.v(LOGTAG,"bitimage is not empty");
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
imageView.setImageBitmap(bitimage);
方法-3:
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
int resid = getResources().getIdentifier("golden_eagle","drawable",getPackageName());
imageView.setImageResource(resid);
//imageView.setImageResource(R.drawable.golden_eagle);
方法4:
//Some class extending AppCompatActivity
asyncTask.execute();
//some asyncTask
onpostExecute(){
//Method:1
//Method:2
//Method:3
}
种所有上述方法用相同的编译错误
12-24 20:16:42.520 1349-1349/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.home.BequestProto, PID: 1349
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.BequestProto/com.example.home.BequestProto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.example.home.BequestProto.MainActivity.onCreate(MainActivity.java:75)
at android.app.Activity.performCreate(Activity.java:6582)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
误差以在这里我设置图像的ImageView行发生结束。 我知道这是一个空指针异常,因为有些地方我忘了初始化所需的对象。我想知道哪一个。
是,findViewById()无法找到这一观点。感谢帮助。 –