我正在创建一个Android应用程序,其中包含一个列表视图,该列表视图由存储在内部存储中的文件填充。当我单击按钮时,它显示错误:
java.lang.ArrayIndexOutOfBoundsException length = 1;索引= 1
这是我点击按钮填充列表视图后的活动:
package com.developer.milanandroid;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Review extends Activity {
ListView List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.review);
ArrayList<String> filesinfolder = GetFiles("/sdcard/Download");
List = (ListView)findViewById(R.id.listView_files);
List.setAdapter(new ArrayAdapter<String>(Review.this, android.R.layout.simple_list_item_1, filesinfolder));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.review, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public ArrayList<String> GetFiles(String directorypath){
ArrayList<String> Myfiles = new ArrayList<String>();
File f = new File(directorypath);
f.mkdirs();
File[] files = f.listFiles();
if(files.length==0){
return null;
}
else{
for(int i=0;i<f.length();i++)
Myfiles.add(files[i].getName());
}
return Myfiles;
}
}
这是我的logcat:
01-02 02:43:25.860: E/AndroidRuntime(13136): FATAL EXCEPTION: main
01-02 02:43:25.860: E/AndroidRuntime(13136): Process: com.developer.milanandroid, PID: 13136
01-02 02:43:25.860: E/AndroidRuntime(13136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.milanandroid/com.developer.milanandroid.Review}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.os.Looper.loop(Looper.java:136)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 02:43:25.860: E/AndroidRuntime(13136): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 02:43:25.860: E/AndroidRuntime(13136): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 02:43:25.860: E/AndroidRuntime(13136): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 02:43:25.860: E/AndroidRuntime(13136): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 02:43:25.860: E/AndroidRuntime(13136): at dalvik.system.NativeStart.main(Native Method)
01-02 02:43:25.860: E/AndroidRuntime(13136): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
01-02 02:43:25.860: E/AndroidRuntime(13136): at com.developer.milanandroid.Review.GetFiles(Review.java:58)
01-02 02:43:25.860: E/AndroidRuntime(13136): at com.developer.milanandroid.Review.onCreate(Review.java:23)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.Activity.performCreate(Activity.java:5231)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 02:43:25.860: E/AndroidRuntime(13136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-02 02:43:25.860: E/AndroidRuntime(13136): ... 11 more
答案 0 :(得分:1)
尝试在for循环中访问错误的引用:
for(int i=0;i<f.length();i++)
替换为:
for(int i=0;i<files.length;i++)