Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:07:29+00:00 2026-06-14T21:07:29+00:00

Currently, I’m trying to create a file manager app on Android, using fragments for

  • 0

Currently, I’m trying to create a file manager app on Android, using fragments for later convenience. As inexperienced as I am, I tend to run into problems, and I can’t figure out this latest one. The app force closes on my the moment I start it. I’m running a Galaxy Nexus in case anyone wanted to know.

There’s an Item class that I’m using here that’s basically just instance variables and get/set methods.

MainActivity:

package tj.apps.files;

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

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.ImageView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class FilesList extends SherlockFragmentActivity implements ListFragment.listFragmentListener {

    // Constructors
    private ItemAdapter aa;
    private ArrayList<Item> array;
    private String extMountState = Environment.getExternalStorageState();
    private String rootPath; 
    private ArrayList<Item> items;
    private FragmentManager fm; 
    private FragmentTransaction ft; 
    private ListFragment listFragment; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.files_list);


        // Set up FragmentManager and FragmentTransaction
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();

        // Comments below is code that doesn't work, as it returns false always on my Galaxy Nexus
        /*
        // Checking whether or not external storage is mounted or not
        // If it isn't mounted, then an ErrorFragment is shown, which is an
        // ImageView saying media isn't mounted and to restart app once it is.
        if (!(extMountState == (Environment.MEDIA_MOUNTED))) {
            ft.add(R.id.container_fragment_listview, new ErrorFragment());
            ft.commit();

        // If it is mounted, the files are retrieved using getFileDirectory,
        // the ListFragment is loaded and set with the ItemAdapter.
        } else {
        */


            File file = Environment.getExternalStorageDirectory();
            rootPath = file.getPath();
            listFragment = new ListFragment();
            ft.add(R.id.container_fragment_listview, listFragment);
            ft.commit();
            listFragment.getFileDir(rootPath);

        }


    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.files_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_search:
            return true;
        case R.id.menu_edit_mode:
            return true;
        case R.id.menu_help:
            return true;
        case R.id.menu_about:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void addNewFragment(String path) {
        ListFragment other = new ListFragment(); 
        ft.replace(R.id.container_fragment_listview, other);
        ft.addToBackStack(null);
        ft.commit(); 
        other.getFileDir(path);
    }

}

ListFragment:

package tj.apps.files;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;

public class ListFragment extends SherlockListFragment {

    // constructors
    private ArrayList<Item> items;
    private ItemAdapter adapter;
    private listFragmentListener lfListener;

    // Get reference to interface in Activity
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        try { 
            lfListener = (listFragmentListener) activity; 
        } catch (ClassCastException e) { 
            throw new ClassCastException (activity.toString() + "must implement listFragmentListener"); 
        }
    }

    // Inflate custom list at R.layout.fragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.list_fragment, container, false);
    }

    // Method to create ListFragment from storage path
    public void getFileDir(String path) {

        // Get the File using the 'path' parameter, then gets the directory at
        // that path, while also initializing the 'items' ArrayList
        File f = new File(path);
        File[] files = f.listFiles();
        items = new ArrayList<Item>();

        // Loops through File[], gets the File at the current position in the
        // File[], gets name/extension/path and puts them to strings,
        // Creates a new Item using those. Those are then added to the 'items'
        // ArrayList
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            String name = file.toString();
            String filePath = file.getPath();
            String extension;

            // If it's a file, then grab the file's extensions (.jpg, .txt,
            // ex.). If it's a folder, get the length of the directory.
            if (file.isFile() == true) {
                extension = name.substring(name.lastIndexOf('.') + 1);
            } else {
                extension = Integer.toString(files[i].listFiles().length)
                        + "files";
            }
            items.add(new Item(name, extension, filePath));
        }

        // initializes ItemAdapter using 'items' and the custom row layout and
        // grabbing the activity this fragment is attached to. Then sets it.
        adapter = new ItemAdapter(getActivity(), R.layout.list_item, items);
        setListAdapter(adapter);
    }

    // On list item click, we check if the file at the position is a folder or not. If it is, we call the listener. 
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        File file = new File (items.get(position).getPath());
        if (file.isDirectory()) {
            lfListener.addNewFragment(items.get(position).getPath());
        }

    }

    //Listener to communicate with Activity it is attached to. Meant to create a new Fragment. 
    public interface listFragmentListener {
        public void addNewFragment(String path);
    }

}

Logcat:

11-25 22:29:19.549: E/AndroidRuntime(12077): FATAL EXCEPTION: main
11-25 22:29:19.549: E/AndroidRuntime(12077): java.lang.RuntimeException: Unable to start activity ComponentInfo{tj.apps.files/tj.apps.files.FilesList}: java.lang.NullPointerException
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.os.Looper.loop(Looper.java:137)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at dalvik.system.NativeStart.main(Native Method)
11-25 22:29:19.549: E/AndroidRuntime(12077): Caused by: java.lang.NullPointerException
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at tj.apps.files.ItemAdapter.<init>(ItemAdapter.java:21)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at tj.apps.files.ListFragment.getFileDir(ListFragment.java:73)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at tj.apps.files.FilesList.onCreate(FilesList.java:58)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.Activity.performCreate(Activity.java:5008)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-25 22:29:19.549: E/AndroidRuntime(12077):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-25 22:29:19.549: E/AndroidRuntime(12077):    ... 11 more

I’m still learning, and I’m sorta stuck on fixing this. Here’s what I’m trying to do:

In the Main Activity, I try to check whether or not the SDCard is mounted or not, if it is, an ErrorFragment pops out. If not, I initialize a ListFragment, add it to a container in the Main Activity’s xml, and then call one of the ListFragment methods on the added Fragment. The method, getFileDir, basically gets a array of Files using the path that was used as the argument in getFileDir. It then gets the name, extension, and path from each File in the array using a for loop, making a new Item and adding it to ‘items’ ArrayList. After the loop, I initialize the Array Adapter using a custom row layout (which I tested as working), the attached Activity’s context using ‘getActivity()’, and the ‘items’ ArrayList. I then call setListAdapter.

EDIT:
Nandesh got it, it works now. Just have another problem. My list items aren’t clickable. It’s probably my ItemAdapter code, which is below:

package tj.apps.files;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<Item> {

int resource; 
Context context;
ArrayList<Item> items;

    public ItemAdapter(Context context, int resource, ArrayList<Item> items) {
        super(context, resource, items);
        this.resource = resource;
        this.context = context;
        this.items = items;
    }

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        ItemHolder holder = null;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(resource, parent, false);

            holder = new ItemHolder();

            convertView.setClickable(true);
            convertView.setFocusable(true);

            holder.text = (TextView) convertView.findViewById(R.id.item_text);
            holder.subtext= (TextView) convertView.findViewById(R.id.item_subtext);

            convertView.setTag(holder);

        } else {
            holder = (ItemHolder) convertView.getTag();
        }

        holder.text.setText(items.get(position).getName());
        holder.subtext.setText(items.get(position).getType());

        return convertView;
    }

    static class ItemHolder {
        ImageView image;
        TextView text;
        TextView subtext;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:07:30+00:00Added an answer on June 14, 2026 at 9:07 pm

    getActivity() is returning null in your case. It can be null if the ListFragment has not been attached to the Activity.

    So move below code to OnAttach

       adapter = new ItemAdapter(getActivity(), R.layout.list_item, items);
       setListAdapter(adapter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently I am debugging the signing of an Android app. And this would be
Currently I am trying to make my app available for iOS4, to increase the
Currently I am working in message compose screen in Android, Using Intent to show
Currently I am using HTML files for parts of my user interface. I display
Currently I am trying to use a bunch of custom perl modules, test.pm as
Currently, my MVC 3 app has a dependency on a static class that is
Currently I am using Amazon Cloudfront to service static objects on my ASP.Net MVC3
Currently, I have a log file of messages in one table in a MySQL
Currently I'm trying to parse some html and return an array with the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.