Edittext上的NullPointerException

时间:2014-09-04 22:54:48

标签: java android nullpointerexception android-edittext

我知道之前已经问过这个问题,但没有人对我的问题有答案。我可以得到错误。

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>    

1 个答案:

答案 0 :(得分:0)

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

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

EditText

然后您可以立即找到 LayoutInflater inflator = youractivity.getLayoutInflater(); rowView = inflator.inflate(the xml file where firstnameupdate is(R.layout.test, null);

EditText