1

Hi every one I'm stuck with doing a simple call to a function. This is my first android app I'm from iPhone. Each time I'm doing something in the onCreate() my app crash.. I'm sure the problem is very simple

public class AndroidReaderActivity extends Activity {

    public void test(){
        File dir=new File("/assets");
        String[] listefichiers;
        int i;
        listefichiers = dir.list();
        for (i = 0; i < listefichiers.length; i++) {
            System.out.println(listefichiers[i]);
        }
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        test();        
    }
}

Thanks you for your help.

Edit: Here are the Cat LOG

05-15 19:52:55.710: E/AndroidRuntime(1956): FATAL EXCEPTION: main
05-15 19:52:55.710: E/AndroidRuntime(1956): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infotel/com.infotel.AndroidReaderActivity}: java.lang.NullPointerException
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.os.Looper.loop(Looper.java:137)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invoke(Method.java:511)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at dalvik.system.NativeStart.main(Native Method)
05-15 19:52:55.710: E/AndroidRuntime(1956): Caused by: java.lang.NullPointerException
05-15 19:52:55.710: E/AndroidRuntime(1956):     at com.infotel.AndroidReaderActivity.test(AndroidReaderActivity.java:14)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at com.infotel.AndroidReaderActivity.onCreate(AndroidReaderActivity.java:23)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.Activity.performCreate(Activity.java:4465)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-15 19:52:55.710: E/AndroidRuntime(1956):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-15 19:52:55.710: E/AndroidRuntime(1956):     ... 11 more
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
Bobyblanco
  • 123
  • 12

4 Answers4

1

From the stacktrace it seems that the problem is when you trying to access /assets directory (which is internally located in the APK)

File dir=new File("/assets");

And when you call dir.list() give the NullPointer since dir could not be instantiated.

What exactly are you trying to do? You want to access the assets directory of your program?

You can list your assets by using following code:

public void test(){
    AssetManager assetManager = null;
    assetManager = getResources().getAssets();

    try {
        // List main folder (/assets) content
        for(String s : assetManager.list("")){
            Log.i("TestActivity", s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Resulting in:

05-11 16:55:08.555: I/TestActivity(13284): de
05-11 16:55:08.555: I/TestActivity(13284): images
05-11 16:55:08.555: I/TestActivity(13284): sounds
05-11 16:55:08.555: I/TestActivity(13284): webkit

You may after that list the content of each directory by giving assetManager.list("de")

Wizche
  • 893
  • 13
  • 32
1

It depends on what you're trying to do. If you are trying to list files in a directory, here is a post that describes how to do that:

How to list files in an android directory?

Community
  • 1
  • 1
Dave
  • 4,949
  • 6
  • 50
  • 73
0

You got a nullpointerexception in the line 14. Look what is this. I think you are not creating the file right.

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
0

There is no absolute path for file exist in Asset folder. The content are packaged in the APK file.

  File dir=new File("/assets");

But you can get an input stream form Asset.

AssetManager am = context.getAssets();

        InputStream is = am.open("/assets/yourfile");
Tai Tran
  • 1,406
  • 3
  • 15
  • 27