J'ai le code comme indiqué ici. Mon problème est une exception NullPointerException sur files.length
for(int i=0; i < files.length; i++){
Cela est dû au fait que j'ai "échoué readDirectory () errno = 13" à
File[] files = f.listFiles();
Mais pourquoi ai-je un échec readDirectory lorsque le chemin d'accès est bon?
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
MISE À JOUR
L'erreur était dans mon manifeste avec les autorisations. Ce que j'avais au début:
<?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>
Et mis à jour:
<?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()
renverra null si le chemin n'existe pas.
Veuillez vérifier votre chemin.
Mise à jour
Android aura également besoin d'une autorisation pour lire certains fichiers. Vous devrez peut-être ajouter ceci à votre manifeste:
<uses-permission Android:name="Android.permission.READ_EXTERNAL_STORAGE" />
Plus d'information:
http://developer.Android.com/reference/Android/Manifest.permission.html
j'ai vérifier l'état
File folderPath = new File (getFilesDir () + "/" + getResources (). GetString (R.string.app_name));
if (folderPath.exists()) {}
pour mon code et cela fonctionne pour moi parce que lorsque ce fichier tente de lire des données, il n'y avait pas de répertoire sur le stockage, ce qui donnait une erreur.
fonctionne maintenant bien