简体   繁体   English

Android EditText中的SetText错误

[英]SetText in EditText Android error

I want to setText in EditView but it will error. 我想在EditView中设置setText,但是会出错。 I dont know why. 我不知道为什么。

This is on OnCreate function. 这是在OnCreate函数上。

name = (EditText) findViewById(R.id.input_name);

String editname = String.valueOf(getName());
MessageTo.message(this,editname);

name.setText(editname);

The string i will set in EditText name. 我将在EditText名称中设置的字符串。

public String getName(){
    int id = 1;
    String data=dbhandler.getName(id);

    return data;
}

If i remove this code from the OnCreate function 如果我从OnCreate函数中删除此代码

name.setText(editname);

It will have no error. 它不会有错误。 The MessageTo.message code is where it pops a dialog of the String editname. MessageTo.message代码将在其中弹出“字符串”编辑名对话框。 It will show the String editname. 它将显示字符串编辑名称。

So i put back the code and change it into this code, 所以我放回代码并将其更改为这段代码,

name.setText("testing");

But it still won't work. 但这仍然行不通。

I dont know why though. 我不知道为什么。 All other sources have EditText.setText("String") as how to setText in EditText but it won't work in my case. 所有其他来源都具有EditText.setText(“ String”)以及如何在EditText中设置setText的功能,但在我的情况下将不起作用。

LOGCAT ERROR: LOGCAT错误:

09-20 21:00:36.677    1436-1436/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.organizer/com.organizer.ManageProfileActivity}: java.lang.NullPointerException

Your EditText name appears to be null. 您的EditText name似乎为空。 In your onCreate method, make sure you are calling setContentView(R.layout.YOUR_LAYOUT) where the layout parameter is an XML layout containing your EditText with the ID R.id.input_name . onCreate方法中,确保要调用setContentView(R.layout.YOUR_LAYOUT) ,其中layout参数是一个XML布局,其中包含ID为R.id.input_name EditText

You assign it later on with 您稍后分配它

name = (EditText) findViewById(R.id.input_name);

but if the layout does not contain this ID, name will be null, thus causing your error. 但是如果布局不包含此ID,则name将为null,从而导致您的错误。

It is correct that you should be using the setText() method on the EditText. 您应该在EditText上使用setText()方法是正确的。

Everything looks correct in terms of your codes, so I would suggest to check for the following things: 就您的代码而言,一切看起来都是正确的,因此,我建议检查以下内容:

  • Are you calling setText anywhere else? 您在其他任何地方都调用setText吗?
  • Is the id of your EditText correct (Is the id of your EditText input_name) 您的EditText的ID是否正确(您的EditText输入的ID的ID)
  • Are you sure you are looking at the right activity. 您确定您正在寻找正确的活动吗?
  • Are you initializing your name variable in the right scope? 您是否在正确的范围内初始化名称变量? (Outside the onCreate Method) (在onCreate方法之外)
  1. Declare the EditText in the xml file 在xml文件中声明EditText
  2. Find the EditText in the activity 在活动中找到EditText
  3. Set the text in the EditText 在EditText中设置文本

And If you check the docs for EditText, you'll find a setText() method. 并且,如果您在文档中检查EditText,则会发现一个setText()方法。 It takes in a String and a TextView.BufferType. 它接受一个String和一个TextView.BufferType。 For example: 例如:

EditText editText =     (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

Or Use +, the string concatenation operator: 或使用+,字符串串联运算符:

 ed = (EditText) findViewById (R.id.box);
    int x = 10;
    ed.setText(""+x);

or 要么

String.valueOf(int):
ed.setText(String.valueOf(x));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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