我需要在android中的sdcard中搜索特定文件并打印结果文件的绝对路径。但是当我跑步时我得到一个错误
"The application Filecare(process com.example.filecare) has stopped unexpectedly. Please try again."
我已经发布了我的源代码和清单。请帮帮我。
FileSearch.java
package com.example.filecare;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;
public class FileSearch extends Activity {
EditText fnn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_search);
fnn=(EditText)findViewById(R.id.editText1);
}
public void Searchstart(View v)
{
searchfn();
}
@SuppressLint("SdCardPath")
public void searchfn()
{
File root = new File("/sdcard/");
String results,fileName = fnn.getText().toString();
// result=(EditText) findViewById(R.id.result);
try {
boolean recursive = true;
Collection files = FileUtils.listFiles(root, null, recursive);
for (Iterator iterator = files.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
if (file.getName().equals(fileName))
System.out.println(file.getAbsolutePath());
//file.delete();
Toast.makeText(getApplicationContext(), "File Exists", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.file_search, menu);
return true;
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.filecare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.filecare.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.filecare.Home"
android:label="@string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.filecare.Login"
android:label="@string/title_activity_login"
android:parentActivityName="com.example.filecare.Home"
android:windowSoftInputMode="adjustResize|stateVisible" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.filecare.Home" />
</activity>
<activity
android:name="com.example.filecare.Register"
android:label="@string/title_activity_register"
android:parentActivityName="com.example.filecare.Home" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.filecare.Home" />
</activity>
<activity
android:name="com.example.filecare.FileOperations"
android:label="@string/title_activity_file_operations"
android:parentActivityName="com.example.filecare.Login" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.filecare.Login" />
</activity>
<activity
android:name="com.example.filecare.FileSearch"
android:label="@string/title_activity_file_search" >
</activity>
</application>
</manifest>
现在我发布了我的新logcat请帮我解决这个问题.. 日志
01-28 16:07:02.153: D/dalvikvm(269): GC_EXTERNAL_ALLOC freed 735 objects / 54888 bytes in 77ms
01-28 16:07:06.273: D/dalvikvm(269): GC_EXTERNAL_ALLOC freed 396 objects / 19976 bytes in 47ms
01-28 16:07:10.613: W/KeyCharacterMap(269): No keyboard for id 0
01-28 16:07:10.623: W/KeyCharacterMap(269): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-28 16:07:15.494: I/dalvikvm(269): Could not find method com.example.filecare.FileOperations.getActionBar, referenced from method com.example.filecare.FileOperations.setupActionBar
01-28 16:07:15.494: W/dalvikvm(269): VFY: unable to resolve virtual method 5062: Lcom/example/filecare/FileOperations;.getActionBar ()Landroid/app/ActionBar;
01-28 16:07:15.494: D/dalvikvm(269): VFY: replacing opcode 0x6e at 0x0006
01-28 16:07:15.494: D/dalvikvm(269): VFY: dead code 0x0009-000d in Lcom/example/filecare/FileOperations;.setupActionBar ()V
01-28 16:07:17.904: I/dalvikvm(269): Could not find method org.apache.commons.io.FileUtils.listFiles, referenced from method com.example.filecare.FileSearch.searchfn
01-28 16:07:17.924: W/dalvikvm(269): VFY: unable to resolve static method 5360: Lorg/apache/commons/io/FileUtils;.listFiles (Ljava/io/File;[Ljava/lang/String;Z)Ljava/util/Collection;
01-28 16:07:17.924: D/dalvikvm(269): VFY: replacing opcode 0x71 at 0x0013
01-28 16:07:17.934: D/dalvikvm(269): VFY: dead code 0x0016-0020 in Lcom/example/filecare/FileSearch;.searchfn ()V
01-28 16:07:17.934: D/dalvikvm(269): VFY: dead code 0x0022-0049 in Lcom/example/filecare/FileSearch;.searchfn ()V
01-28 16:07:22.343: D/AndroidRuntime(269): Shutting down VM
01-28 16:07:22.343: W/dalvikvm(269): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-28 16:07:22.353: E/AndroidRuntime(269): FATAL EXCEPTION: main
01-28 16:07:22.353: E/AndroidRuntime(269): java.lang.IllegalStateException: Could not execute method of the activity
01-28 16:07:22.353: E/AndroidRuntime(269): at android.view.View$1.onClick(View.java:2072)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.view.View.performClick(View.java:2408)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.view.View$PerformClick.run(View.java:8816)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.os.Handler.handleCallback(Handler.java:587)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-28 16:07:22.353: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 16:07:22.353: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 16:07:22.353: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-28 16:07:22.353: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-28 16:07:22.353: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
01-28 16:07:22.353: E/AndroidRuntime(269): Caused by: java.lang.reflect.InvocationTargetException
01-28 16:07:22.353: E/AndroidRuntime(269): at com.example.filecare.FileSearch.Searchstart(FileSearch.java:28)
01-28 16:07:22.353: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 16:07:22.353: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 16:07:22.353: E/AndroidRuntime(269): at android.view.View$1.onClick(View.java:2067)
01-28 16:07:22.353: E/AndroidRuntime(269): ... 11 more
01-28 16:07:22.353: E/AndroidRuntime(269): Caused by: java.lang.NoClassDefFoundError: org.apache.commons.io.FileUtils
01-28 16:07:22.353: E/AndroidRuntime(269): at com.example.filecare.FileSearch.searchfn(FileSearch.java:39)
01-28 16:07:22.353: E/AndroidRuntime(269): ... 15 more
01-28 16:07:30.174: I/Process(269): Sending signal. PID: 269 SIG: 9
我发布了布局xml文件 * * activity_file_search.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FileSearch" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="42dp"
android:onClick="Searchstart"
android:text="Search" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:ems="10" />
</RelativeLayout>
答案 0 :(得分:0)
需要获得对存储进行读写的权限。
在清单上的应用程序标记之前插入代码:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我不知道java代码抱歉。