首页 \ 问答 \ 使用ListView中的值在新的Dialog中填充EditText(Fill EditText in new Dialog with values from ListView)

使用ListView中的值在新的Dialog中填充EditText(Fill EditText in new Dialog with values from ListView)

嘿,我正在尝试使用ListView值填充新Dialog中的EditText 。 问题是我得到了

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object

我对从ListView获得的值进行Toast并且它可以工作,它返回特定值。 当我尝试调用.setText(ValueFromListView)它给出了错误,我不明白为什么。 谁能帮我这个? 这是代码:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.setTitle("Edit item...");
    dialog.setContentView(R.layout.custom_dialog_edit_delete);
    dialog.show();

    Button button_UPDATE = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_UPDATE);
    Button button_CANCEL = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_CANCEL);
    Button button_DELETE = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_DELETE);

    // GET PRODUCT REFERENCES
    Cursor cursor = (Cursor) parent.getItemAtPosition(position);
    String productName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_PRODUCT));
    String productPrice = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_PRICE));
    String productType = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_TYPE));
    Toast.makeText(MainActivity.this, productName + " " + productPrice + " " + productType, Toast.LENGTH_SHORT).show(); 

    EditText prodField = (EditText) findViewById(R.id.custom_dialog_edit_delete__productName);
    prodField.setText(productName); // This is the line that i get the null pointer exception
}

Hey i am trying to fill an EditText in a new Dialog with values from a ListView. The problem is that i get

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object

I put a Toast on the values obtained from the ListView and it works, it returns the specific value. When i try to call .setText(ValueFromListView) it gives the error and i don't understand why. Can anyone help me with this? Here is the code:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.setTitle("Edit item...");
    dialog.setContentView(R.layout.custom_dialog_edit_delete);
    dialog.show();

    Button button_UPDATE = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_UPDATE);
    Button button_CANCEL = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_CANCEL);
    Button button_DELETE = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_DELETE);

    // GET PRODUCT REFERENCES
    Cursor cursor = (Cursor) parent.getItemAtPosition(position);
    String productName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_PRODUCT));
    String productPrice = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_PRICE));
    String productType = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_TYPE));
    Toast.makeText(MainActivity.this, productName + " " + productPrice + " " + productType, Toast.LENGTH_SHORT).show(); 

    EditText prodField = (EditText) findViewById(R.id.custom_dialog_edit_delete__productName);
    prodField.setText(productName); // This is the line that i get the null pointer exception
}

原文:https://stackoverflow.com/questions/35913003
更新时间:2019-06-21 02:04

最满意答案

EditText在对话框中可用,因此在创建EditText对象时必须提供yout对话框的参考。

EditText prodField = (EditText) dialog.findViewById(R.id.custom_dialog_edit_delete__productName);

EditText is available inside your dialog so you must give reference of yout dialog when creating object of EditText.

EditText prodField = (EditText) dialog.findViewById(R.id.custom_dialog_edit_delete__productName);
2016-03-10

相关问答

更多

如何从EditText插入文本到数据库中?(How to insert a text from an EditText into a database?)

那么,因为我不能看到MainActivity布局的XML,这里是最有可能导致NPE。 final EditText text = (EditText)findViewById(R.id.editText); 当你在你的活动视图层次结构中搜索你的编辑文本时(假设从对话框创建中显示),你应该在对话视图层次结构中搜索它。 .setView(R.layout.custom_view) 在那里,您将该视图设置为对话框的视图,这可能是导致异常的原因。 尝试在onClick方法中修改对编辑文本 ...

从EditText过滤ListView结果(Filter ListView results from an EditText)

我看到你是从一个名为“tableItems”的对象创建列表适配器 - 假设它是一个List<MapLocationDetailsActivityListViewTableItem> ,这应该工作: inputSearch.TextChanged += (sender, e) => { listView.Adapter = new MapLocationDetailsActivityAdapter ( this, tableItems.Where(it ...

Android - 正确执行自定义对话框中的自定义列表视图(Android - Executing a custom listview in a custom dialog properly)

你在做什么? 你将需要膨胀布局文件dialog_main ,找到ListView ,为它设置一个适配器和一个OnItemClickListener。 之后,您可以使用对话框的setContentView(View)方法获取要显示的列表。 private void showDialog(){ final Dialog dialog = new Dialog(this); View view = getLayoutInflater().inflate(R.layout.dialog ...

如何从片段中获取EditText(How to get EditText from fragment)

尝试这样做: LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.add_subcontractor_form, null); alert.setView(dialogView); EditText editText = (EditText) dialogView.findViewById(R.id.et_sub_name); Try t ...

setHintTextColor()在EditText中(setHintTextColor() in EditText)

使用此来更改提示颜色。 - editText.setHintTextColor(getResources().getColor(R.color.white)); 解决您的问题 - editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){ //do so ...

从EditText字段获取数据(Getting data from EditText fields)

edittext t1 = (edittext) textEntryView.findviewbyid(R.id.vsname); String s = t1.gettext().toString(); edittext t1 = (edittext) textEntryView.findviewbyid(R.id.vsname); String s = t1.gettext().toString();

在AdapterView问题中从另一个布局传递EditText值作为IntentExtra(Passing an EditText Value as IntentExtra from a different Layout in AdapterView Issue)

您应该使用ListView.setOnItemClickListener() 。 回调返回单击列表中的位置和视图。 该位置用于检索数据对象Questionere 。 可以使用findViewById()检索EditText 。 ListView list = (ListView) findViewById(R.id.listview); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Overrid ...

AlertDialog中的多个EditText对象(Multiple EditText objects in AlertDialog)

对话框只包含一个根视图,这就是为什么setView()覆盖第一个EditText。 该解决方案很简单,将所有内容放在一个ViewGroup中,例如LinearLayout: Context context = mapView.getContext(); LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.VERTICAL); // Add a TextView here for t ...

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)