0

Sorry, It's a repetitive question, but i haven't find my answer yet. To make a FileChooser in my Android application i used this. So I added 2 jar files in my Properties-> Java Build path( one of them is android-support-v4.jar and the other is a handmade library(afilechooser)), but when it comes to run i face with these errors in onResume() method.

1-Is it possible that there is a problem with libs?

2-What is your solution?

3-Is there any problem with the FileChooserActivity?

The class is:

public class FileChooserActivity extends FragmentActivity implements
        OnBackStackChangedListener {

    public static final String PATH = "path";
    public static final String EXTERNAL_BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();

    private FragmentManager mFragmentManager;
    private BroadcastReceiver mStorageListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, R.string.storage_removed, Toast.LENGTH_LONG).show();
            finishWithResult(null);
        }
    };

    private String mPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.chooser);

        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.addOnBackStackChangedListener(this);

        if (savedInstanceState == null) {
            mPath = EXTERNAL_BASE_PATH;
            addFragment(mPath);
        } else {
            mPath = savedInstanceState.getString(PATH);
        }

        setTitle(mPath);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterStorageListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerStorageListener();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString(PATH, mPath);
    }

    @Override
    public void onBackStackChanged() {
        mPath = EXTERNAL_BASE_PATH;

        int count = mFragmentManager.getBackStackEntryCount();
        if (count > 0) {
            BackStackEntry fragment = mFragmentManager
                    .getBackStackEntryAt(count - 1);
            mPath = fragment.getName();
        }

        setTitle(mPath);
    }

    /**
     * Add the initial Fragment with given path.
     * 
     * @param path The absolute path of the file (directory) to display.
     */
    private void addFragment(String path) {
        FileListFragment explorerFragment = FileListFragment.newInstance(mPath);
        mFragmentManager.beginTransaction()
                .add(R.id.explorer_fragment, explorerFragment).commit();
    }

    /**
     * "Replace" the existing Fragment with a new one using given path.
     * We're really adding a Fragment to the back stack.
     * 
     * @param path The absolute path of the file (directory) to display.
     */
    private void replaceFragment(String path) {
        FileListFragment explorerFragment = FileListFragment.newInstance(path);
        mFragmentManager.beginTransaction()
                .replace(R.id.explorer_fragment, explorerFragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(path).commit();
    }

    /**
     * Finish this Activity with a result code and URI of the selected file.
     * 
     * @param file The file selected.
     */
    private void finishWithResult(File file) {
        if (file != null) {
            Uri uri = Uri.fromFile(file);
            setResult(RESULT_OK, new Intent().setData(uri));
            finish();
        } else {
            setResult(RESULT_CANCELED); 
            finish();
        }
    }

    /**
     * Called when the user selects a File
     * 
     * @param file The file that was selected
     */
    protected void onFileSelected(File file) {
        if (file != null) {
            mPath = file.getAbsolutePath();

            if (file.isDirectory()) {
                replaceFragment(mPath);
            } else {
                finishWithResult(file); 
            }
        } else {
            Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Register the external storage BroadcastReceiver.
     */
    private void registerStorageListener() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
        registerReceiver(mStorageListener, filter);
    }

    /**
     * Unregister the external storage BroadcastReceiver.
     */
    private void unregisterStorageListener() {
        unregisterReceiver(mStorageListener);
    }
}

The errors are:

06-04 04:30:57.547: E/AndroidRuntime(605): FATAL EXCEPTION: main
06-04 04:30:57.547: E/AndroidRuntime(605): java.lang.NoClassDefFoundError: com.ipaulpro.afilechooser.R$layout
06-04 04:30:57.547: E/AndroidRuntime(605):  at com.ipaulpro.afilechooser.FileChooserActivity.onCreate(FileChooserActivity.java:65)
06-04 04:30:57.547: E/AndroidRuntime(605):  at android.app.Activity.performCreate(Activity.java:5104)
06-04 04:30:57.547: E/AndroidRuntime(605):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-04 04:30:57.547: E/AndroidRuntime(605):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

. . .

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
  • 1
    Seems resources of afilechooser (handmade) is not included in the jar file (because they shouldn't be included in there). Have You tried to make handmade as library project and setup it's dependency in Yours one (instead of using jar)? – sandrstar Jun 04 '13 at 05:07
  • wether you included the jar of your library or the library proj itself ? if u included the jar you cant access the layout files if you added it as a library proj from properties _> android-> add -> then there is a possibility of this to come if the lib proj is not in the same folder ass your curr proj – Athul Harikumar Jun 04 '13 at 05:09

3 Answers3

0

In Eclipse:

  1. Right click on your Project
  2. Select Properties -> Java Build Path-> Order and Export
  3. Check the Android Private Libraries and then click OK button.

enter image description here

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

You should import the library as an existing Android project into Eclipse workspace. Check in the properties that it is defined as a library.

Then you have to import it in your project. This is done in Properties => Android => Add (in the library section). Choose the library, and then it should be working

dleal
  • 642
  • 6
  • 19
0

Please Replace android-support-v4.jar file. and put ActivityNot Found Exception.

    Intent target = FileUtils.createGetContentIntent();
    Intent intent = Intent.createChooser(
            target, getString(R.string.chooser_title));
    try {
        startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {

    }