从一个活动切换到下一个活动时,我会收到一些错误。切换到的活动使用自定义列表视图,我怀疑这里的错误导致我的所有问题。我想知道是否将空的arraylist传递给我的自定义适配器会导致这种情况,因为它从文件读取,这可能导致它返回null。
这是我的错误(来自Eclipse的logcat):
01-02 06:13:26.239: E/File not found(1765): java.io.FileNotFoundException:
/data/data/com.example.partyorganiser/files/PeopleDetailsFile: open failed: ENOENT (No such file or directory)
01-02 06:13:26.259: E/AndroidRuntime(1765): FATAL EXCEPTION: main
01-02 06:13:26.259: E/AndroidRuntime(1765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.partyorganiser/com.example.partyorganiser.AddParty_PeopleList}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.partyorganiser.PeopleDetails[]
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.os.Looper.loop(Looper.java:137)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 06:13:26.259: E/AndroidRuntime(1765): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 06:13:26.259: E/AndroidRuntime(1765): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 06:13:26.259: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 06:13:26.259: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 06:13:26.259: E/AndroidRuntime(1765): at dalvik.system.NativeStart.main(Native Method)
01-02 06:13:26.259: E/AndroidRuntime(1765): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.partyorganiser.PeopleDetails[]
01-02 06:13:26.259: E/AndroidRuntime(1765): at com.example.partyorganiser.AddParty_PeopleList.onCreate(AddParty_PeopleList.java:37)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.Activity.performCreate(Activity.java:5133)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 06:13:26.259: E/AndroidRuntime(1765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-02 06:13:26.259: E/AndroidRuntime(1765): ... 11 more
感谢您的帮助!! :)
编辑:代码段:
package com.example.partyorganiser;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AddParty_PeopleList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView people_listview = (ListView) findViewById(R.layout.person_list_item);
setContentView(R.layout.activity_add_party__people_list);
ArrayList<PeopleDetails> people_list = Readpeopledetails();
if(people_list != null){
final peoplelist_customadapter adapter = new peoplelist_customadapter(this, R.layout.person_list_item, people_list );
people_listview.setAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_party__people_list, menu);
return true;
}
public ArrayList<PeopleDetails> Readpeopledetails(){
String filereadfrom = "";///declares a string to read file contents into. then split into peopledetails.
///this was copied straight from AddPeople. Could place in another static class, and then access it from both, but this was a very quick
///quite untidy way of doing it.
ArrayList<PeopleDetails> peopledetails_array = new ArrayList<PeopleDetails>();
try{
InputStream filestream = openFileInput("PeopleDetailsFile");
if (filestream != null){
InputStreamReader inputStreamReader = new InputStreamReader(filestream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
filereadfrom = stringBuilder.toString();
///using two delimiters: ";" and ","
///split based on ";" to get people as a string, put in array
String[] parsearray = filereadfrom.split(";");
for(String personstring: parsearray){
String[] split = personstring.split(",");
PeopleDetails peopledetails_unit = new PeopleDetails("", "");
peopledetails_unit.name= split[0];
peopledetails_unit.number = split[1];
peopledetails_array.add(peopledetails_unit);
}
filestream.close();
inputStreamReader.close();
return peopledetails_array; }
}
catch(FileNotFoundException e) {
Log.e("File not found" , e.toString());
return peopledetails_array;
}
catch(IOException e){
Log.e("Can't read file" , e.toString());///this is useful
for debugging, to figure out the problem.
return peopledetails_array;
}
return peopledetails_array;
}
}