我想从我的移动存储中将图片加载到应用的图像视图中。可以肯定的是,我将图像保存在内部和外部存储中。但是在编译时,我得到一个空指针异常。我还将相同的图像放在drawable文件夹中,并尝试从那里设置imageview。但面临同样的问题。有人能帮我一下吗!。我正在添加我尝试过的代码片段。
方法-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的行。 我知道它是一个空指针异常,因为有些地方我忘了初始化所需的对象。我想知道哪一个。
答案 0 :(得分:1)
"方法4:"显示您正在使用异步任务执行。我想布局发生了变化,并试图在之后设置图像。
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
if (imageView != null)
imageView.setImageBitmap(outputImageBitmap);
仅当View存在时才会设置图像
答案 1 :(得分:0)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
这似乎来自“方法-3”,您正在呼叫setImageResource()
。
此错误表示此行是问题所在:
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
在所有四种情况下, imageView
都是null
,因为findViewById()
无法找到此小部件。
答案 2 :(得分:0)
仔细检查ImageView
是否为sample_image
。在您的布局中找不到它