This question already has an answer here:
这个问题已经有了答案:
- What is a NullPointerException, and how do I fix it? 12 answers
- 什么是NullPointerException,我如何修复它?12个答案
(There are 12 more same question asked like this but none of them answer my question.. As its belong to code Exception not every repeated question has same issue).
)还有12个同样的问题,但没有一个能回答我的问题。由于它属于代码异常,不是每个重复的问题都有相同的问题)。
I am trying to fetch images from custom folder on drawing app which I have created. It already get all the file and fetching all images in custom_adapter but when I use bitmap-decode it will throw the error Unable to decode stream: java.lang.NullPointerException
.
我正在尝试从我创建的绘图应用的自定义文件夹中获取图像。它已经获取了所有的文件,并在custom_adapter中获取所有图像,但是当我使用bitmap-decode时,它将抛出无法解码流的错误:java.lang.NullPointerException。
Here is the code:
这是代码:
MainActivity.class
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collection_grid);
String path = Environment.getExternalStorageDirectory().toString() + "/" + getString(R.string.Drawing);
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "Size: " + file.length);
for (int i=0; i < file.length; i++)
{
Log.d("Files", "FileName:" + file[i].getName());
}
String [] FilePathStrings = (new String[file.length]);
GridView g1=(GridView)findViewById(R.id.grid);
g1.setAdapter(new Custom_grid(Grid_Collection.this,FilePathStrings));
}
Custom_Adapter.class
Custom_Adapter.class
public class Custom_grid extends BaseAdapter {
String [] result;
private static LayoutInflater inflater=null;
Context context;
public Custom_grid(Grid_Collection grid_collection,String [] file) {
result= file;
context = grid_collection;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// Log.e("LIST SIZE", "" + result.length);
return result.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
class Holder{
ImageView iv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder;
View vi=convertView;
if(convertView == null){
vi = inflater.inflate(R.layout.imageview,null);
holder = new Holder();
holder.iv= (ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else {
holder = (Holder) vi.getTag();
}
Log.e("LIST POSITION", "" + result[position]);
Bitmap bmp = BitmapFactory.decodeFile(result[position]);
holder.iv.setImageBitmap(bmp);
return vi;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
}
Why it showing error? Code is all okay.
为什么它显示错误?代码都是好的。
LOGCAT::
LOGCAT::
10-17 13:59:38.852 19817-19817/com.example.dell.drawdemo D/AbsListView: checkAbsListViewlLogProperty get invalid command
10-17 13:59:38.853 19817-19817/com.example.dell.drawdemo D/Files: Path: /storage/emulated/0/Drawing App
10-17 13:59:38.862 19817-19817/com.example.dell.drawdemo D/Files: Size: 13
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:984f456b-91ee-4cd7-893e-faeb44fcc1c1.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:79fdc1fa-c5bb-4e88-aa26-94563b0d72bd.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:a4537e3d-c708-45e5-82fd-8c43e48878d3.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:cdaa2564-6db0-4b77-ad33-c06eb37978a2.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:41d782a1-7682-4e69-a0e3-7bf3adc9dc3f.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:bd06264a-d91e-43b2-b17b-e7d2746642cc.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:d1fcb76f-c90b-44ac-98f2-03d0dd255532.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:86c8f75d-4532-4100-b239-e3e67fe45dec.png
10-17 13:59:38.863 19817-19817/com.example.dell.drawdemo D/Files: FileName:811e7856-0dc7-4a5a-ac4c-371f39d5a16f.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:48c634e3-a3d0-43d6-9387-9ce3dba7c874.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:6057691d-f96e-4554-921e-316cda99b034.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:1c7674be-860c-4e76-a5bb-21648bd3d9ea.png
10-17 13:59:38.864 19817-19817/com.example.dell.drawdemo D/Files: FileName:3665b64a-e2eb-4d81-ab62-d40ab0cc74ac.png
10-17 13:59:38.865 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{42434500 token=android.os.BinderProxy@42433c48 {com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection}}
10-17 13:59:38.865 19817-19817/com.example.dell.drawdemo V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42435aa0 I.E..... R.....ID 0,0-0,0}
10-17 13:59:38.870 19817-19817/com.example.dell.drawdemo V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{42456a48 com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection,ident = 1}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42435aa0 V.E..... R.....ID 0,0-0,0}
10-17 13:59:38.872 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{42434500 token=android.os.BinderProxy@42433c48 {com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection}}
10-17 13:59:38.891 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.893 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.927 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62a76d68) (w:544, h:960, f:1)
10-17 13:59:38.928 19817-19817/com.example.dell.drawdemo I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
10-17 13:59:38.928 19817-19817/com.example.dell.drawdemo I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
10-17 13:59:38.928 19817-19817/com.example.dell.drawdemo I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
10-17 13:59:38.929 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x62a76d68) (w:544 h:960 f:1)
10-17 13:59:38.932 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62a79b68) (w:544, h:960, f:1)
10-17 13:59:38.934 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: setViewport 540x960 <0x62a79cf0>
10-17 13:59:38.935 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.935 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.938 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.938 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.941 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.941 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.944 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.944 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.947 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.948 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.951 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.951 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.954 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.954 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.957 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.957 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.960 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.960 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.963 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.963 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.966 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.966 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.969 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.969 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.972 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.972 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.976 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:38.976 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:38.985 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:38.985 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:38.989 19817-19817/com.example.dell.drawdemo D/AbsListView: onWindowFocusChanged: hasWindowFocus=true, this=android.widget.GridView{42439dc8 VFED.VC. .F....I. 0,0-540,922 #7f0c0067 app:id/grid}
10-17 13:59:38.990 19817-19817/com.example.dell.drawdemo V/InputMethodManager: onWindowFocus: android.widget.GridView{42439dc8 VFED.VC. .F....I. 0,0-540,922 #7f0c0067 app:id/grid} softInputMode=272 first=true flags=#1810100
10-17 13:59:38.990 19817-19817/com.example.dell.drawdemo V/InputMethodManager: START INPUT: android.widget.GridView{42439dc8 VFED.VC. .F....I. 0,0-540,922 #7f0c0067 app:id/grid} ic=null tba=android.view.inputmethod.EditorInfo@42470a08 controlFlags=#105
10-17 13:59:39.002 19817-19817/com.example.dell.drawdemo E/LIST POSITION: null
10-17 13:59:39.002 19817-19817/com.example.dell.drawdemo E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
10-17 13:59:39.008 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x6207c688) (w:544, h:960, f:1)
10-17 13:59:39.016 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:39.016 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:39.030 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62f4e0b8) (w:544, h:960, f:1)
10-17 13:59:39.032 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: Flushing caches (mode 0)
10-17 13:59:39.033 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x618d6d18) (w:544 h:960 f:1)
10-17 13:59:39.033 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x5d127550) (w:544 h:960 f:1)
10-17 13:59:39.034 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x62873990) (w:544 h:960 f:1)
10-17 13:59:39.034 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: close handle(0x62f4e0b8) (w:544 h:960 f:1)
10-17 13:59:39.346 19817-19817/com.example.dell.drawdemo V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =ViewRoot{424234f8 com.example.dell.drawdemo/com.example.dell.drawdemo.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{42316338 I.E..... R....... 0,0-540,960}
10-17 13:59:39.346 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-STOP_ACTIVITY_HIDE handled : 0 / android.os.BinderProxy@42303440
10-17 13:59:40.102 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x5d12efa8) (w:544, h:960, f:1)
10-17 13:59:40.104 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.105 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.119 19817-19817/com.example.dell.drawdemo D/GraphicBuffer: create handle(0x62f4e0b8) (w:544, h:960, f:1)
10-17 13:59:40.121 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.122 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.135 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.136 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.152 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.153 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.169 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.169 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.187 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.188 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.201 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.202 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.218 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.218 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.235 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.235 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.251 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.252 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.268 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.269 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.286 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.287 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.301 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.302 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.318 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.318 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.336 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.337 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:40.351 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x62a79cf0>
10-17 13:59:40.351 19817-19817/com.example.dell.drawdemo D/OpenGLRenderer: finish <0x62a79cf0>
10-17 13:59:54.746 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{42434500 token=android.os.BinderProxy@42433c48 {com.example.dell.drawdemo/com.example.dell.drawdemo.Grid_Collection}}
10-17 13:59:54.781 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@42433c48
10-17 13:59:54.812 19817-19817/com.example.dell.drawdemo D/ActivityThread: ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@42433c48
10-17 13:59:55.024 19817-19817/com.example.dell.drawdemo V/InputMethodManager: START INPUT: android.widget.GridView{42439dc8 VFED.VC. .F...... 0,0-540,922 #7f0c0067 app:id/grid} ic=null tba=android.view.inputmethod.EditorInfo@42481718 controlFlags=#100
1 个解决方案
#1
1
Maybe this will help you :)
With:
也许这能帮助你:)
File file[] = f.listFiles();
文件文件[]= f.listFiles();
You get the list of files that you output on the Log.
Than you instantiate FilePathStrings: (But now it is an empty array!)
您将获得在日志中输出的文件列表。而不是实例化filepathstring:(但现在它是一个空数组!)
String [] FilePathStrings = (new String[file.length]);
字符串[]FilePathStrings =(新字符串[file.length]);
Please add this line before calling setAdapter(...)
请在调用setAdapter(…)之前添加这一行。
...
for (int i=0; i < file.length; i++){
FilePathStrings[i] = file[i].getAbsolutePath();
}
...
Please edit your code and tell me if this solved the issue.
请编辑您的代码并告诉我这是否解决了问题。
#1
1
Maybe this will help you :)
With:
也许这能帮助你:)
File file[] = f.listFiles();
文件文件[]= f.listFiles();
You get the list of files that you output on the Log.
Than you instantiate FilePathStrings: (But now it is an empty array!)
您将获得在日志中输出的文件列表。而不是实例化filepathstring:(但现在它是一个空数组!)
String [] FilePathStrings = (new String[file.length]);
字符串[]FilePathStrings =(新字符串[file.length]);
Please add this line before calling setAdapter(...)
请在调用setAdapter(…)之前添加这一行。
...
for (int i=0; i < file.length; i++){
FilePathStrings[i] = file[i].getAbsolutePath();
}
...
Please edit your code and tell me if this solved the issue.
请编辑您的代码并告诉我这是否解决了问题。