Android的点击数,方法异常 [英] Android onlick-Method exception
问题描述
您好我收到以下异常,我不知道为什么:找不到在活动课android.view.ContextThemeWrapper为onclick处理方法checkPassword(视图)的视图类android.widget.Button ID为bPassword
Hello I am getting the following exception and I don't know why: Could not find a method checkPassword(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'bPassword'
我使用的是Activtiy为对话框。用户必须在对话框中输入密码,如果它是正确的另一个活动应该叫。这是我的code:
I am using a Activtiy as Dialog. The user must type a password in the Dialog, if it is correct another activity should be called. Here is my code:
我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter your Password:"
android:id="@+id/dText"/>
<EditText
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/ePassword"
android:layout_marginLeft="-1dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/bPassword"
android:onClick="checkPassword" />
</LinearLayout>
我的Java类:
My Java Class:
package com.example.RemindMe;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class DialogActivity extends Activity {
private String password = "test";
private EditText ePassword;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.activity_dialog);
dialog.setTitle("Note Activity is password protected");
dialog.setCancelable(true);
textView = (TextView) dialog.findViewById(R.id.dText);
textView.setText("Message");
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0){
finish();
}
});
}
public void checkPassword(View view){
switch (view.getId()){
case R.id.bPassword:
String pwEditText = ePassword.getText().toString();
if(pwEditText.equals(password)){
//If password correct, start another activity
startActivity (new Intent(getApplicationContext(), NoteActivity.class));
} else {
textView.setText("Wrong Password, PLS Type it again");
}
break;
}
}
}
我知道密码不应该被用作明文等。这是仅用于测试目的。我planninng在共享$ P $粉煤散列和存储密码
I know that the password should not be used as a clear text and so on. This is only for testing purpose. I am planninng to hash and store the password in the sharedpref
推荐答案
为了让您的活动
的外观对话框
添加以下行&LT;活性GT;
相应的活动
在的manifest.xml
。
To give your Activity
the appearance of a Dialog
add the following line to the <activity>
tag of the appropriate Activity
in your manifest.xml
.
android:theme="@android:style/Theme.Dialog"
然后就可以删除所有的对话框
code和它应该工作,因为你的的onClick
是在活动
本身,而不是对话框
。只是不要忘了添加的setContentView(...)
在的onCreate()
。
then you can remove all of the dialog
code and it should work since your onClick
is in the Activity
itself and not the Dialog
. Just don't forget to add setContentView(...)
in onCreate()
.
这篇关于Android的点击数,方法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!