2015-11-05 75 views
-1

我從仿真器中收到一個「不幸」的應用程序名稱「已停止」消息。我認爲腳本找不到文件夾或無法讀取文件名。 res文件夾的照片已附加。我嘗試了像「res/drawable」「/ res/drawable /」等,但我得到了相同的結果。 我在清單文件中使用了下面的權限,但無濟於事。java.lang.NullPointerException:嘗試獲取與'無法讀取空數組'有關的空數組的長度

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

我檢查了所有stackoverflaw相關的鏈接,但無法解決我的問題。

package chessactivepgnviewer.com.pgnviewer; 

import java.io.File; 
import java.util.ArrayList; 

/** 
* Created by serhat on 04.11.2015. 
*/ 
public class klasor { 
    private String drawable; 
    private ArrayList<File> files; 

    public void listf(String drawable, ArrayList<File> files) { 
     File directory; 
     directory = new File(drawable); 

     // get all the files from a directory 
     File[] fList = directory.listFiles(); 
     for (File file : fList) { 
      if (file.isFile()) { 
       files.add(file); 
      } else if (file.isDirectory()) { 
       listf(file.getAbsolutePath(), files); 
      } 
     } 
    } 
} 

我的主要活動文件請特別注意我給了文件的路徑

package chessactivepgnviewer.com.pgnviewer; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 

import java.io.File; 

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.satranc_tahtasi); 

     File folder = new File("D:/Android_Dosyalar/Proje/PGNViewer/app/src/main/res/drawable"); 
     File[] listOfFiles = folder.listFiles(); 

     for (int i = 0; i < listOfFiles.length; i++) { 
      if (listOfFiles[i].isFile()) { 
       System.out.println("File " + listOfFiles[i].getName()); 
      } else if (listOfFiles[i].isDirectory()) { 
       System.out.println("Directory " + listOfFiles[i].getName()); 
      } 
     } 
    } 
} 

這是logcat的

11-05 22:29:41.577 2215-2215/? I/art: Not late-enabling -Xcheck:jni (already on) 
11-05 22:29:41.678 2215-2215/chessactivepgnviewer.com.pgnviewer D/AndroidRuntime: Shutting down VM 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: FATAL EXCEPTION: main 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: Process: chessactivepgnviewer.com.pgnviewer, PID: 2215 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{chessactivepgnviewer.com.pgnviewer/chessactivepgnviewer.com.pgnviewer.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5257) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to get length of null array 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at chessactivepgnviewer.com.pgnviewer.MainActivity.onCreate(MainActivity.java:20) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:5990) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer 

回答

0

你應該修正你想要的路徑訪問,更改此

File folder = new File("D:/Android_Dosyalar/Proje/PGNViewer/app/src/main/res/drawable"); 

的路徑定位設備,例如:

File folder = new File("/data/chessactivepgnviewer.com.pgnviewer/"); 
+0

謝謝你的回答。我會檢查並返回...問候 - 阿齊茲S. Kural –

+0

你有權限清單文件中的READ_EXTERNAL_STORAGE? – Alisson

+0

很抱歉,我把我的答案放在了錯誤的地方。 Regards –

0

您的問題是,這些行:

File folder = new File("D:/Android_Dosyalar/Proje/PGNViewer/app/src/main/res/drawable"); 
File[] listOfFiles = folder.listFiles(); 

爲listOfFiles,似乎空。

如果你想獲得所有的可繪製的列表,你可能想試試這個:

Field[] drawables = chessactivepgnviewer.com.pgnviewer.R.drawable.class.getFields(); 

for (Field f : drawables) { 
    try { 
     System.out.println("R.drawable." + f.getName()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

謝謝你的回答。我會檢查並返回...問候 –

+0

'文件夾'不是'null'。 'listOfFiles'可以是'null',但不是'folder'。 – Tom

+0

你好,我想我把評論放在錯誤的地方,或者放在那裏。我試過你的以上片段,這是我得到的: \t 我只是得到一個黑色的屏幕。不過,我可以從logcat看到它正在看到這些文件。你可以弄清楚出了什麼問題?? ..... 11-05 23:22:33.189 10517-10542/chessactivepgnviewer.com.pgnviewer W/EGL_emulation :eglSurfaceAttrib not implemented 11-05 23:22:33.189 10517-10542/chessactivepgnviewer.com.pgnviewer W/OpenGLRenderer:無法在曲面0xb431b680上設置EGL_SWAP_BEHAVIOR,錯誤= EGL_SUCCESS - –

0

文件夾不存在。所以,folder.listFiles()返回null。

如果您在apk中放置了一張圖片,請使用assets文件夾。