2014-09-04 77 views
1

我知道這個問題以前已經被問過了,但沒有人對我的問題有過答案。我可以得到這個錯誤。Edittext上的NullPointerException

log.d顯示我正在檢索firstname之前我setText(firstname)和那個困惑我。從logcat的

我的代碼和錯誤

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 

} 

錯誤信息

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 

XML含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文件:

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

如果你確實點到線#** ** 114在'MainActivity.java'文件,然後'firstnameupdate'是'null'。 – 2014-09-04 22:59:43

+0

嘗試替換這一行:'firstnameupdate =(EditText)view.findViewById(R.id.firstnameupdate);'with:firstnameupdate =(EditText)findViewById(R.id.firstnameupdate);' – alfasin 2014-09-04 23:03:09

+0

@PM 77-1 that why我已經'firstnameupdate.setText(firstname)',所以它應該設置爲'名字' – user3476925 2014-09-04 23:08:07

回答

0

在onItemClick的View似乎沒有找到您R.id.firstnameupdate這意味着你的ListView當前View犯規包含EditText名稱爲R.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); 
+0

'LayoutInflater inflator = youractivity.getLayoutInflater()'中的''activity'''' ? 「onClickListener」是哪一個? – user3476925 2014-09-05 13:26:10

+0

是@ user3476925 – 2014-09-08 00:47:53