对不起,这是一个重复的问题,但我还没有找到我的答案。为了在我的 Android 应用程序中创建 FileChooser,我使用了这个。所以我在我的Properties-> Java Build 路径中添加了 2 个 jar 文件(其中一个是android-support-v4.jar
,另一个是手工库(afilechooser
)),但是当涉及到运行时,我在onResume()
方法中遇到了这些错误。
1-库可能有问题吗?
2-你的解决方案是什么?
3- FileChooserActivity 有问题吗?
课程是:
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);
}
}
错误是:
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)
. . .