完成后的Android错误()

时间:2013-12-12 00:21:26

标签: android listview

这是我第一次在本网站上提问,所以如果我犯了任何错误,请告诉我如何以更好的方式做到这一点。我下次会考虑你的意见。请忽略语法错误,因为我通常说德语。

我的程序的主要活动包括listview,如果我点击一个项目,另一个活动开始,您可以编辑数据。单击“确定”按钮后,活动应该消失,主要活动应显示更新的列表视图。我的问题是单击确定按钮后程序崩溃。

感谢您的帮助

EditEmployee的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_employee);
    Bundle bundle = getIntent().getExtras();
    int index = bundle.getInt("Index");
    Log.d( LOG_TAG, "Index: "+index);
    employee = Repository.getInstance().getEmployee(index);
    etFirstName = (EditText)findViewById(R.id.editTextFirstName);
    etLastName = (EditText)findViewById(R.id.editTextLastName);
    etBirthdate = (EditText)findViewById(R.id.editTextBirthDate);
    etSvnr = (EditText)findViewById(R.id.editTextSvnr);
    etFirstName.setText(employee.getFirstName());
    etLastName.setText(employee.getLastName());
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
    String birthdayString = formatter.format(employee.getBirthDate());
    etBirthdate.setText(birthdayString);
    etSvnr.setText(((Integer)employee.getSvnr()).toString());
}

public void onClick(View view) throws ParseException{
    employee.setFirstName(etFirstName.getText().toString());
    employee.setLastName(etLastName.getText().toString());
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
    Date birthDate = formatter.parse(etBirthdate.getText().toString());
    employee.setBirthDate(birthDate);
    employee.setSvnr(Integer.parseInt(etBirthdate.getText().toString()));
    finish();
}

MainActivity的代码

package at.htl.employeelist;

import android.app.ListActivity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    static final int REQUEST_CODE_EDIT_EMPLOYEE_ACTIVITY = 4711;
    private static final String LOG_TAG = null;

    // private static final String LOG_TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] employeeStrings;
        try{
            employeeStrings = Repository.getInstance().getEmployeeStrings();
            ArrayAdapter<String> arrayAdapter =
                    new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, employeeStrings);
            setListAdapter(arrayAdapter);
        } catch (ParseException e){
            Log.e(LOG_TAG, "Repositoryerror: "+e.getMessage());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        TextView textViewSelected = (TextView) findViewById(R.id.textViewSelected);
        String selectedString = getResources().getString(R.string.textViewSelected);
        textViewSelected.setText(selectedString + Repository.getInstance().getEmployeeStrings()[position].toString());
        Intent intentEditEmployee = new Intent(this, EditEmployee.class);
        intentEditEmployee.putExtra("Index", position);
        startActivity(intentEditEmployee);
    }

    @Override
    protected void onResume() {
        String[] employeeStrings;
        employeeStrings = Repository.getInstance().getEmployeeStrings();
        ArrayAdapter<String> arrayAdapter =
                new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, employeeStrings);
        setListAdapter(arrayAdapter);
        super.onResume();
    }   
}

来自LogCat的信息

12-12 00:05:04.950: D/AndroidRuntime(12407): Shutting down VM
12-12 00:05:04.950: W/dalvikvm(12407): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
12-12 00:05:04.960: E/AndroidRuntime(12407): FATAL EXCEPTION: main
12-12 00:05:04.960: E/AndroidRuntime(12407): java.lang.IllegalStateException: Could not execute method of the activity
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.view.View$1.onClick(View.java:2162)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.view.View.performClick(View.java:2534)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.view.View$PerformClick.run(View.java:9210)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.os.Handler.handleCallback(Handler.java:587)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.os.Looper.loop(Looper.java:123)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.app.ActivityThread.main(ActivityThread.java:3701)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.reflect.Method.invoke(Method.java:507)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at dalvik.system.NativeStart.main(Native Method)
12-12 00:05:04.960: E/AndroidRuntime(12407): Caused by: java.lang.reflect.InvocationTargetException
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.reflect.Method.invoke(Method.java:507)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at android.view.View$1.onClick(View.java:2157)
12-12 00:05:04.960: E/AndroidRuntime(12407):    ... 11 more
12-12 00:05:04.960: E/AndroidRuntime(12407): Caused by: java.lang.NumberFormatException: unable to parse '04.07.1981' as integer
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.Integer.parse(Integer.java:383)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.Integer.parseInt(Integer.java:372)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at java.lang.Integer.parseInt(Integer.java:332)
12-12 00:05:04.960: E/AndroidRuntime(12407):    at at.htl.employeelist.EditEmployee.onClick(EditEmployee.java:49)
12-12 00:05:04.960: E/AndroidRuntime(12407):    ... 14 more

1 个答案:

答案 0 :(得分:1)

你正在接受NumberFormatException。错误是由于以下行:

employee.setSvnr(Integer.parseInt(etBirthdate.getText().toString()));

您尝试将dd.mm.yyyy解析为整数。例如:这里04.07.1981到整数。 .不是整数。 ddmmYYYY,即04071981可能会被解析为整数。