gpt4 book ai didi

java - OnEditorActionListener 中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 07:09:13 24 4
gpt4 key购买 nike

在我的 Android 项目中按 Enter 时出现 NullPointerException。我用谷歌搜索,人们遇到了同样的问题,因为他们没有设置 findViewByIt。不幸的是我明白了。发生了什么事 - 为什么我得到异常?

   @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
textPassword = (EditText) findViewById(R.id.editText1);
buttonLogin = (Button) findViewById(R.id.button1);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
PrintToast(textPassword.getText().toString());
}
});
textPassword.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
try
{
if ((event.getAction() == KeyEvent.ACTION_DOWN))
{
buttonLogin.performClick();
}
}
catch ( Exception e) {
PrintToast(e.toString());
}
return false;
}

});



}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainPage" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/PIN_label_HelloMsg"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="@string/PIN_button_AcceptPIN" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:text="@string/PIN_label_EnterPIN"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:inputType="numberPassword" >

<requestFocus />
</EditText>

</RelativeLayout>

最佳答案

试试这个

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
textPassword = (EditText) findViewById(R.id.editText1);
buttonLogin = (Button) findViewById(R.id.button1);
textPassword.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
try
{
if ((event.getAction() == KeyEvent.ACTION_DOWN))
{
buttonLogin.performClick();
}
}
catch ( Exception e) {
PrintToast(e.toString());
}
return false;
}

});

[编辑]这很好用

Button buttonLogin;
EditText textPassword ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textPassword = (EditText) findViewById(R.id.editText1);
buttonLogin = (Button) findViewById(R.id.button1);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, textPassword.getText().toString(), Toast.LENGTH_LONG).show();
}
});
textPassword.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {

@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {

if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here

buttonLogin.performClick();
Log.e("Called","Called OKKKKKK");
return true;
}

Log.e("Called","Called");

return false;

}

});



}

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com