我有这个代码巫婆返回文件的内容:
private ArrayList<String> readFromFile1(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesNames2.bjk"));
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}
我这样称呼它:
System.out.println("FILE CONTENT: " + readFromFile1(this));
并且一切都运行正常。
现在在另一项活动中,我使用的代码几乎相同,但它不会运行。
这是:
@SuppressLint("NewApi")
public class FavoriteJokes extends Activity {
public static ArrayAdapter<String> adapter;
public static ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite_jokes);
final GlobalsHolder globals = (GlobalsHolder)getApplication();
TextView text = (TextView) findViewById(R.id.txtJoke);
text.setText(globals.getList().get(globals.clickedPosition));
ArrayList<String> jokeNamesList = new ArrayList<String>();
jokeNamesList.addAll(readFromFile1(this));
System.out.println("FILE CONTENT: " + readFromFile1(this));
globals.setFavJokeNamesList(readFromFile1(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite_jokes, menu);
return true;
}
/* Read file's content */
private ArrayList<String> readFromFile1(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesNames2.bjk"));
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}
return list;
}
}
这是错误输出:
03-03 10:50:21.055: E/AndroidRuntime(1038): FATAL EXCEPTION: main
03-03 10:50:21.055: E/AndroidRuntime(1038): Process: com.gs.britishjokes, PID: 1038
03-03 10:50:21.055: E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gs.britishjokes/com.gs.britishjokes.FavoriteJokes}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.os.Handler.dispatchMessage(Handler.java:102)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.os.Looper.loop(Looper.java:137)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.lang.reflect.Method.invoke(Method.java:515)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-03 10:50:21.055: E/AndroidRuntime(1038): at dalvik.system.NativeStart.main(Native Method)
03-03 10:50:21.055: E/AndroidRuntime(1038): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.util.ArrayList.get(ArrayList.java:308)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.gs.britishjokes.FavoriteJokes.onCreate(FavoriteJokes.java:31)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.Activity.performCreate(Activity.java:5243)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-03 10:50:21.055: E/AndroidRuntime(1038): ... 11 more
我真的无法理解我的代码有什么问题。我很确定我错过了一小部分,但我找不到它。你能帮我推吗?
答案 0 :(得分:1)
错误日志(logcat)表示IndexOutOfBoundsException异常:对空数组的元素0的访问。
似乎是这一行造成了问题:text.setText(globals.getList().get(globals.clickedPosition));
也许您可以检查globals.getList()的大小?