1

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);*/

}

2 Answers2

0

Are you sure that the /raw/Emprendimiento directory contains any files?

If you get a NPE on filesName = new String[fileList.length];, it means that fileList is null, which means that the listFiles() method returns null.

gosr
  • 4,593
  • 9
  • 46
  • 82
0

Since the files are in the app, and can't be changed without publishing, I would recommend just manually creating a list of string that contains the name of all the files.

Then with the name you can get the id with :

int resId = getResources().getIdentifier(resourceName, "raw", getApplicationContext().getPackageName());

That will be the same integer as R.raw.xxxx, which will be used to read the file.

There is a way -dirty- to iterate through resources, but you'll get the integer value and not the name (in case).


Or, if you put your files in the assets folder, you can list them like that :

String[] fileNames =getAssets().list("myFolder");
Community
  • 1
  • 1
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
  • Dang, I was planning to put (in several folders) around 100 files. I'm new at programming, Is this thing that difficult? :'( – Castor Troy Sep 13 '14 at 16:17
  • There should be no need to retrieve a list of all resources since you have to put them inside you app and can access them with the R class. If you have a lots of files, maybe you should name them `something_0001`, `something_0002`, so you can just iterate through the names easily. – Stephane Mathis Sep 13 '14 at 16:23
  • Hmmm. The thing I'm trying to do is create several folders (one per category) and then show a listview of each category with the files on that folder to open them from there. If I put them all in a unique folder, how could I categorize them? . Is not possible to do it the other way by tweaking the code? Thanks a lot for your time. I really apreciate it :) – Castor Troy Sep 13 '14 at 16:31
  • You can have subfolders in the asset folder because it is apart from the res folder. – Stephane Mathis Sep 13 '14 at 16:33
  • Ok, I put what you said there but it's wrong. How should I tweak the block? Sorry, I'm really new with this. Thanks again! – Castor Troy Sep 13 '14 at 16:45