简体   繁体   English

将文本设置为edittext(例外)

[英]Set text to edittext (exception)

I have : 我有 :

 nameET=(EditText) findViewById(R.id.editText1);
 nameET.setText("kkkk");

inside Oncreat() function, the code give exception as below: 在Oncreat()函数中,代码给出如下异常: 在此处输入图片说明

You are getting NullPointerException So there may be various reason for that. 您将收到NullPointerException因此可能有多种原因。

  • First check that you have declare your activity in the manifest.xml 首先检查您是否已在manifest.xml声明了您的活动

  • Second check the name of the edittext in your layout is it same you as editText1 . 其次检查名称edittext在你的布局是相同的,你作为editText1

  • Third are you calling setContentView(R.layout.your layout where you declare your edittext); 第三,您调用setContentView(R.layout.your layout where you declare your edittext); in onCreate(Bundle savedInstanceState) method of the activity. 在活动的onCreate(Bundle savedInstanceState)方法中。

If all the above you checked is fine then just clean your project and run. 如果以上所有检查均正常,则只需清理项目并运行。 This will work. 这将起作用。

Thanks 谢谢

检查您的xml布局,并确保存在editText1

Two Quick Checks, 两次快速检查

  • Did you declare the activity in manifest.xml 您是否在manifest.xml声明了活动
  • Are you using the same name editText1 as specified in xml and is it EditText Defined 您是否使用与xml中指定的名称相同的editText1 ,是否已定义EditText

setContentView is missing. setContentView丢失。 Please check this in your code 请检查您的代码

          View inflatedView = getLayoutInflater().inflate(R.layout.activity_sliding, null);

        nameET=(EditText)inflatedView.findViewById(R.id.editText1);
                 nameET.setText("kkkk");

=============================================== ===============================================

use the activity which contain R.id.editText1 instead of activity_sliding 使用包含R.id.editText1的活动而不是activity_sliding

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

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