我正在尝试在/ raw上创建的文件夹上显示文件,应用程序崩溃了。这是代码(logcat表示nullpointer由行“filesName = new String [fileList.length];”
触发)package com.estudiable.estudiable;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import java.io.File;
public class Categoria_Emprendimiento extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categoria__emprendimiento);
File dir = new File("/raw/Emprendimiento");
File[] fileList = dir.listFiles();
String[] filesName;
filesName = new String[fileList.length];
for (int i = 0; i < filesName.length; i++) {
filesName[i] = fileList[i].getName();
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filesName);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.categoria__emprendimiento, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the logCat is:
java.lang.RuntimeException: Unable to start activityComponentInfo{com.estudiable.
estudiable/com.estudiable.estudiable.Categoria_Emprendimiento}
: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.estudiable.estudiable.Categoria_Emprendimiento.onCreate(Categoria_Emprendimiento.java:22)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
感谢您的时间。
编辑尝试建议的解决方案(这些文件现在位于资源文件夹的子文件夹中。它给我一个错误。我应该如何正确使用它?)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categoria__emprendimiento);
File dir = new File("assets/Emprendimiento");
File[] fileList = dir.listFiles();
String[] filesName = getAssets().list("assets/Emprendimiento");
for (int i = 0; i < filesName.length; i++) {
filesName[i] = fileList[i].getName();
}
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filesName);
编辑2 从getAssets()到分号用红色下划线标出。我错过了什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categoria__emprendimiento);
String[] filesName = getAssets().list("app/src/main/assets/Emprendimiento");
/* File dir = new File("/raw/Emprendimiento");
//File[] fileList = dir.listFiles();
//String[] filesName = new String[fileList.length];
//for (int i = 0; i < filesName.length; i++) {
//filesName[i] = fileList[i].getName();
//new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filesName);*/
}
答案 0 :(得分:0)
您确定/raw/Emprendimiento
目录包含任何文件吗?
如果您在filesName = new String[fileList.length];
上获得NPE,则表示fileList
为null
,这意味着listFiles()
方法会返回null
。
答案 1 :(得分:0)
由于文件在应用程序中,并且在没有发布的情况下无法更改,我建议只手动创建包含所有文件名称的字符串列表。
然后使用名称,您可以获得ID:
int resId = getResources().getIdentifier(resourceName, "raw", getApplicationContext().getPackageName());
这将是与R.raw.xxxx
相同的整数,它将用于读取文件。
有一种方法 - 脏 - 迭代资源,但你会得到整数值,而不是名称(in case)。
或者,如果您将文件放在assets文件夹中,则可以将它们列为:
String[] fileNames =getAssets().list("myFolder");