FATAL EXCEPTION: main
Process: a2day.a2day, PID: 28475
java.lang.RuntimeException: Unable to start activity ComponentInfo{a2day.a2day/a2day.a2day.Explore}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at a2day.a2day.Explore.imageReader(Explore.java:75)
at a2day.a2day.Explore.onCreate(Explore.java:28)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
以上是我试图从我的目录加载图片时接近的错误...下面我正在添加java类,我必须尝试在Android设备上加载图像。
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class ViewImage extends AppCompatActivity {
ImageView iv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_image);
String f = getIntent().getStringExtra("img");
iv2 = (ImageView) findViewById(R.id.imageView2);
iv2.setImageURI(Uri.parse(f));
}
}
public class Explore extends ActionBarActivity {
GridView gv;
ArrayList<File> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explore);
list = imageReader(Environment.getExternalStorageDirectory());
gv = (GridView) findViewById(R.id.gridView);
gv.setAdapter(new GridAdapter());
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getApplicationContext(), ViewImage.class).putExtra("img", list.get(position)));
}
});
}
private class GridAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.my_grid, parent ,false);
ImageView iv = (ImageView) convertView.findViewById(R.id.singleGrid);
iv.setImageURI(Uri.parse(getItem(position).toString()));
return convertView;
}
}
ArrayList<File> imageReader(File root){
ArrayList<File> a = new ArrayList<>();
File[] files = root.listFiles();
for(int i =0; i < files.length; i++) {
if(files[i].isDirectory()) {
a.addAll(imageReader(files[i]));
} else {
if(files[i].getName().endsWith(".jpg")) {
a.add(files[i]);
}
}
}
return a;
}
}
有人可以帮我解决这个问题吗?我不知道它有什么问题,它给我的压力太大,因为我没有时间完成这项工作,因为我还有其他需要完成的工作。