I'm trying to show the files on a folder created on /raw and the app crashes. Here is the code (The logcat says the nullpointer is triggered by the line "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)
Thanks for your time.
EDIT trying the proposed solution (The files are now in a subfolder in the assets folder. It gaves me an error. How should I use it properly?)
@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);
EDIT 2 From getAssets() to the semicolon is underlined in red. What am I missing?
@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);*/
}