gpt4 book ai didi

java - 运行方法时出现很多错误

转载 作者:行者123 更新时间:2023-12-01 18:38:19 25 4
gpt4 key购买 nike

好的,我已经开始在 Eclipse 中使用 Java 进行编程,并制作一个 Android 应用程序。在我的一项 Activity 中,我保存了人们的详细信息 - 姓名和手机号码(以便稍后向他们发送短信)。然而,当执行该方法时,会引发大量错误。该方法如下所示,通过单击按钮(标准内容)来执行:

它还触发另一种方法,写入文件:

public void addpeople_action_save(View view) {
EditText et_addpeople_name = (EditText) findViewById(R.id.addpeople_name);
EditText et_addpeople_number = (EditText) findViewById(R.id.addpeople_number);
String person_name = et_addpeople_name.getText().toString();///parses from the EditText to a string.
String person_number = et_addpeople_number.getText().toString();

///Next section is writing the two strings to a internal file, as a CSV format.
///eg: Bill, 0123567898
///this will actually append the string to an arraylist of strings, before writing it back to the file.
ArrayList<PeopleDetails> peopledetails_return =Readpeopledetails();///creates an arraylist containing people's details
///read from file
PeopleDetails person_details = new PeopleDetails("", "");
person_details.name = person_name;
person_details.number = person_number;
peopledetails_return.add(person_details);
peopledetails_return = sortList(peopledetails_return);
peopledetails_write(peopledetails_return);

/
///or will write a new file if it doesn't exist.
///New method(PeopleNumber, PeopleName, Filename)
///will write to file.
}

以及错误:

01-02 08:47:20.290: E/AndroidRuntime(1966): FATAL EXCEPTION: main
01-02 08:47:20.290: E/AndroidRuntime(1966): java.lang.IllegalStateException: Could not execute method of the activity
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.view.View$1.onClick(View.java:3633)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.view.View.performClick(View.java:4240)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.view.View$PerformClick.run(View.java:17721)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.os.Handler.handleCallback(Handler.java:730)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.os.Looper.loop(Looper.java:137)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 08:47:20.290: E/AndroidRuntime(1966): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 08:47:20.290: E/AndroidRuntime(1966): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 08:47:20.290: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 08:47:20.290: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 08:47:20.290: E/AndroidRuntime(1966): at dalvik.system.NativeStart.main(Native Method)
01-02 08:47:20.290: E/AndroidRuntime(1966): Caused by: java.lang.reflect.InvocationTargetException
01-02 08:47:20.290: E/AndroidRuntime(1966): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 08:47:20.290: E/AndroidRuntime(1966): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 08:47:20.290: E/AndroidRuntime(1966): at android.view.View$1.onClick(View.java:3628)
01-02 08:47:20.290: E/AndroidRuntime(1966): ... 11 more
01-02 08:47:20.290: E/AndroidRuntime(1966): Caused by: java.lang.NullPointerException
01-02 08:47:20.290: E/AndroidRuntime(1966): at com.example.partyorganiser.AddPeople.addpeople_action_save(AddPeople.java:63)
01-02 08:47:20.290: E/AndroidRuntime(1966): ... 14 more

感谢您的帮助;)

编辑:阅读器方法代码:

public ArrayList<PeopleDetails> Readpeopledetails(){        String filereadfrom = "";





try{ InputStream filestream = openFileInput("PeopleDetailsFile"); if (filestream != null){///first time file is not created. Later will be created, when writing back to the file.
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
///then split all strings in that array, based on "," and place each into the array peopledetails
String[] parsearray = filereadfrom.split(";");
ArrayList<PeopleDetails> peopledetails_array = new ArrayList<PeopleDetails>();

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);

///neatly initializes and writes to the peopledetails_array in one section.
}
filestream.close();
inputStreamReader.close();



return peopledetails_array; } }




catch(FileNotFoundException e) {
Log.e("File not found" , e.toString());
}
catch(IOException e){
Log.e("Can't read file" , e.toString());





} return null;

最佳答案

此位置出现 NullPointerException:

com.example.partyorganiser.AddPeople.addpeople_action_save(AddPeople.java:63)

很难说更多

关于java - 运行方法时出现很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com