我知道以前曾问过这个问题,但没人能回答我的问题。希望我能得到这个错误。

log.d显示在我firstname之前正在检索setText(firstname),这让我感到困惑。

我的代码和错误

UserList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            // add data here
            fname = (TextView) findViewById(R.id.firstname);
            lname = (TextView) findViewById(R.id.lastname);
            agee = (TextView) findViewById(R.id.age);

            firstname=fname.getText().toString();
            lastname=lname.getText().toString();


            Log.d("filter",firstname);
            Log.d("filter",lastname);

            firstnameupdate = (EditText) view.findViewById(R.id.firstnameupdate);
            lastnameupdate= (EditText) view.findViewById(R.id.lastnameudpate);

            firstnameupdate.setText(firstname);  <==== here is the error
            lastnameupdate.setText(lastname);

            Intent update = new Intent(getApplicationContext(),update.class);
            startActivity(update);

        }

    });

    // add onitemlongclick here

}


来自logcat的错误消息

 E/AndroidRuntime(6455): FATAL EXCEPTION: main
 E/AndroidRuntime(6455): java.lang.NullPointerException
 E/AndroidRuntime(6455):    at com.exampl1.quayomobility.MainActivity$2.onItemClick(MainActivity.java:114)
 E/AndroidRuntime(6455):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
 E/AndroidRuntime(6455):    at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
 E/AndroidRuntime(6455):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3078)
 E/AndroidRuntime(6455):    at android.widget.AbsListView$1.run(AbsListView.java:4161)
 E/AndroidRuntime(6455):    at android.os.Handler.handleCallback(Handler.java:615)
 E/AndroidRuntime(6455):    at android.os.Handler.dispatchMessage(Handler.java:92)


代码在onCreate(Bundle savedInstanceState)

这就是log.d给我的:

09-05 01:36:04.737: D/filter(6455): alex
09-05 01:36:04.737: D/filter(6455): bondaro


包含firstnameupdate的XML

<EditText
    android:id="@+id/firstnameupdate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="-10dp"
    android:ems="10" >

    <requestFocus />
</EditText>


mainActivity文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<Button
    android:id="@+id/addmem_bt_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="ADD USER" />

<ListView
    android:id="@+id/memberList_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="2dp" >
</ListView>

</LinearLayout>

最佳答案

onItemClick中的View似乎找不到您的R.id.firstnameupdate,这意味着列表视图中的当前View不包含名称为EditTextR.id.firstnameupdate

如果EditText R.id.firstnameupdate在其他xml文件中。您需要使用Layout inflater

  LayoutInflater inflator = youractivity.getLayoutInflater();
  rowView = inflator.inflate(the xml file where firstnameupdate is(R.layout.test, null);


然后您现在可以找到EditText

firstnameupdate = (EditText) rowView.findViewById(R.id.firstnameupdate);
lastnameupdate= (EditText) rowView.findViewById(R.id.lastnameudpate);

10-04 13:10