java - Edittext 上的 NullPointerException

标签 java android nullpointerexception android-edittext

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

log.d 显示我在 setText(firstname) 之前检索了 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 savingInstanceState)

这就是 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>

主 Activity 文件:

<?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 这意味着 ListView 中当前的 View 不包含名为 R.id.firstnameupdateEditText

如果 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);

关于java - Edittext 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25676216/

相关文章:

java - 如何读取 Firebase 实时数据库中的嵌套数据( HashMap )

java - 最终引用对象被修改

java - 在获得返回值之前中断方法

Java NullPointErexception Bukkit 插件

java - 什么是NullPointerException,我该如何解决?

java - 有没有办法使用变量来命名列表?

Android:桌面上的 adb 拉文件

android - 应用自定义样式时,如何防止用户粘贴按钮内部?

android - 在 Presentation Display 中使用演示文稿上下文显示 Toast 消息

java - 将修改后的xml保存到Android内部存储中