简体   繁体   中英

NullPointerException on Edittext

i know that this question have been asked before,but no one had an answer for my question.hope i can get the error .

log.d shows that am retrieving firstname before i setText(firstname) and that what confused me.

my code and the error

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

}

error messages from 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)

the code is in onCreate(Bundle savedInstanceState)

that is what the log.d give me :

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

XML containing firstnameupdate

<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 file :

<?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>    

the View in onItemClick doesn't seem to locate your R.id.firstnameupdate which means the current View in your listview doesnt contain the EditText with the name R.id.firstnameupdate .

if the EditText R.id.firstnameupdate is in other xml file. you need to use Layout inflater

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

then you can locate now the EditText

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM