我想要的只是在editText
中设置一些文字。我不想在xml中使用android:text="bla bla bla"
因为我得到了用户输入的东西。
Y是这个简单的东西不适合我。我的应用程序成功构建但崩溃了。
aaaa.setText("sss");
我尝试了一个新项目,只是为了验证它的工作原理。好吧,它没有。
我的档案:-(新项目)
package com.rishav.testing;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText aaaa=(EditText)findViewById(R.id.aaaa);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aaaa.setText("sss");
}
}
布局: -
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rishav.testing.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="300dp"
android:id="@+id/aaaa"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
顺便说一句,我正在使用AS 3.0 canary 3 sdk工具26.0.0
错误Logcat: -
06-13 04:02:47.368 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.371 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.373 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.375 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.377 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.647 14469-14469/com.rishav.t2 W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
06-13 04:02:56.393 14469-14469/com.rishav.t2 I/hifiwi: Signup
06-13 04:02:57.126 14469-14469/com.rishav.t2 I/hifiwi: working atleast
06-13 04:02:57.126 14469-14469/com.rishav.t2 I/hifiwi: working atleast
06-13 04:02:57.128 14469-14469/com.rishav.t2 D/AndroidRuntime: Shutting down VM
--------- beginning of crash
06-13 04:02:57.129 14469-14469/com.rishav.t2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rishav.t2, PID: 14469
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.rishav.t2.SigningActivity.deft(SigningActivity.java:208)
at com.rishav.t2.MyDemoListener.onClick(SigningActivity.java:222)
at android.support.design.widget.Snackbar$1.onClick(Snackbar.java:255)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
答案 0 :(得分:3)
您可以将视图定义为字段,但不能在其中初始化。
技术原因:在类初始化时没有为Activity定义上下文
public class MainActivity extends AppCompatActivity {
private EditText aaaa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aaaa=(EditText)findViewById(R.id.aaaa);
aaaa.setText("sss");
}
}
如果您计划使用其他方法,这也是更好的方法
答案 1 :(得分:2)
只需在onCreate中定义EditText,然后在setText中定义
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText aaaa=(EditText)findViewById(R.id.aaaa);
aaaa.setText("sss");
}
}
答案 2 :(得分:2)
您正在尝试在屏幕上创建editText之前访问它。移动这一行
EditText aaaa=(EditText)findViewById(R.id.aaaa);
之后
setContentView(R.layout.activity_main);
EditText aaaa=(EditText)findViewById(R.id.aaaa);
aaaa.setText("sss");
答案 3 :(得分:0)
接下来我要在Kotlin上使用
edit_text_view.text!!.append("some_text")
没有检查,但是我认为在Java中会有类似的东西-
edit_text_view.setText(edit_text_view.getText().append("some_text"));
如果要设置新文本,请首先使用clear()
功能(对于Editable -> edit_text_view.getText()
)