androidBeginner
androidBeginner

Reputation: 197

Android onclick-Method exception

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'

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:

My XML File:

<?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>

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;
        }
    }
}

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

Upvotes: 0

Views: 181

Answers (3)

Pankaj Arora
Pankaj Arora

Reputation: 10274

you have to define which theme you want in you activity like this:

android:theme="@android:style/Theme.Dialog"

Upvotes: 0

ramaral
ramaral

Reputation: 6179

The android:onClick="checkPassword" is only usable for buttons used in activity, because the checkPassword method was declare in activity.

You should avoid instantiating Dialog directly. Instead, use AlertDialog.

Remove that button from xml and use the ok button from AlertDialog.

Upvotes: -1

codeMagic
codeMagic

Reputation: 44571

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"

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().

Upvotes: 3

Related Questions