I have the code as shown here. My Problem is a NullPointerException at files.length
for(int i=0; i < files.length; i++){
It is caused because i have a "fail readDirectory() errno=13" at
File[] files = f.listFiles();
But why do I have a readDirectory fail when the path is good?
package com.example.androidexplorer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
Log.v("Path: ", dirPath);
Log.v("BEFORE", "Before Reading Fail...");
File[] files = f.listFiles();
Log.v("AFTER", "After Reading Fail...");
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
Log.v("CRASH", "1 Line before crash");
for(int i=0; i < files.length; i++){
Log.v("AFTER CRASH", "1 Line after crash");
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
}
LogCat:
> 04-23 15:35:34.084: D/ResourcesManager(20672): creating new > AssetManager and set to > /data/app/com.example.androidexplorer-1/base.apk 04-23 15:35:34.104: > I/art(20672): Created application space > /data/dalvik-cache/arm/data@[email protected]@[email protected] > at 0x76eb0000~0x76f15ff8 04-23 15:35:34.104: I/art(20672): Loaded art > file: > /data/dalvik-cache/arm/data@[email protected]@[email protected] > 04-23 15:35:34.194: V/BitmapFactory(20672): > DecodeImagePath(decodeResourceStream3) : > res/drawable-xxhdpi-v4/ic_ab_back_holo_dark_am.png 04-23 15:35:34.204: > V/BitmapFactory(20672): DecodeImagePath(decodeResourceStream3) : > res/drawable-xxhdpi-v4/sym_def_app_icon.png 04-23 15:35:34.234: > D/AbsListView(20672): Get MotionRecognitionManager 04-23 15:35:34.244: > V/BitmapFactory(20672): DecodeImagePath(decodeResourceStream3) : > res/drawable-xhdpi-v4/ic_launcher.png 04-23 15:35:34.254: > V/Path:(20672): /storage/emulated/0 04-23 15:35:34.254: > V/BEFORE(20672): Before Reading Fail... 04-23 15:35:34.254: > E/File(20672): fail readDirectory() errno=13 04-23 15:35:34.254: > V/AFTER(20672): After Reading Fail... 04-23 15:35:34.254: > V/CRASH(20672): 1 Line before crash 04-23 15:35:34.254: > D/AndroidRuntime(20672): Shutting down VM 04-23 15:35:34.254: > E/AndroidRuntime(20672): FATAL EXCEPTION: main 04-23 15:35:34.254: > E/AndroidRuntime(20672): Process: com.example.androidexplorer, PID: > 20672 04-23 15:35:34.254: E/AndroidRuntime(20672): > java.lang.RuntimeException: Unable to start activity > ComponentInfo{com.example.androidexplorer/com.example.androidexplorer.MainActivity}: > java.lang.NullPointerException: Attempt to get length of null array > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > android.app.ActivityThread.access$900(ActivityThread.java:172) 04-23 > 15:35:34.254: E/AndroidRuntime(20672): at > android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > android.os.Handler.dispatchMessage(Handler.java:102) 04-23 > 15:35:34.254: E/AndroidRuntime(20672): at > android.os.Looper.loop(Looper.java:145) 04-23 15:35:34.254: > E/AndroidRuntime(20672): at > android.app.ActivityThread.main(ActivityThread.java:5834) 04-23 > 15:35:34.254: E/AndroidRuntime(20672): at > java.lang.reflect.Method.invoke(Native Method) 04-23 15:35:34.254: > E/AndroidRuntime(20672): at > java.lang.reflect.Method.invoke(Method.java:372) 04-23 15:35:34.254: > E/AndroidRuntime(20672): at > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 04-23 > 15:35:34.254: E/AndroidRuntime(20672): Caused by: > java.lang.NullPointerException: Attempt to get length of null array > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > com.example.androidexplorer.MainActivity.getDir(MainActivity.java:57) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > com.example.androidexplorer.MainActivity.onCreate(MainActivity.java:31) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > android.app.Activity.performCreate(Activity.java:6221) 04-23 > 15:35:34.254: E/AndroidRuntime(20672): at > android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) > 04-23 15:35:34.254: E/AndroidRuntime(20672): at > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611) > 04-23 15:35:34.254: E/AndroidRuntime(20672): ... 10 more
UPDATE
Error was in my manifest with the permissions. What I had at the beginning:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spicysoftware.infoid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="android.permission.READ_EXTERNAL_STORAGE">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".sysinfo"></activity>
<activity android:name=".daten"></activity>
<activity android:name=".storage"></activity>
<activity android:name="org.achartengine.GraphicalActivity"> </activity>
</application>
</manifest>
And Updated:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spicysoftware.infoid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".sysinfo"></activity>
<activity android:name=".daten"></activity>
<activity android:name=".storage"></activity>
<activity android:name="org.achartengine.GraphicalActivity"> </activity>
</application>
</manifest>
f.listFiles()
will return null if the path does not exist.
Please check your path.
Update
Android will also require a permission to read certain files. Maybe you need to add this to your manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
More information:
http://developer.android.com/reference/android/Manifest.permission.html
All you need to do is go into your manifest file and under the backup line just paste it
android:requestLegacyExternalStorage="true"
Done :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With