2011-11-15 37 views
6

Vì vậy, tôi nhận được lỗi nàyKhông thể tìm thấy phương pháp onClick

11-15 16:55:40.617: E/AndroidRuntime(316): java.lang.IllegalStateException: Could not find a method ingresarBtnClick(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'ingresarButton' 

này được bố trí của tôi xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1" android:id="@+id/Login"> 
    <TextView android:layout_width="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Número de Lecturista" android:layout_height="wrap_content"></TextView> 
    <EditText android:inputType="number" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/numLecEditText" android:maxLength="4"> 
     <requestFocus></requestFocus>  
    </EditText> 
    <TextView android:layout_width="wrap_content" android:id="@+id/textView2" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="PIN"></TextView> 
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:password="true" android:id="@+id/pinEditText" android:maxLength="4"></EditText> 
    <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content"> 
     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ingresar" android:id="@+id/ingresarButton" android:onClick="ingresarBtnClick"></Button> 
     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Salir" android:id="@+id/salirButton" android:onClick="salirBtnClick"></Button> 
     <Button android:id="@+id/opcionesButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Opciones" android:onClick="opcionesBtnClick" ></Button> 
    </TableRow> 
</LinearLayout> 

Và đây là mã của tôi

import android.app.Dialog; 
import android.view.View; 
import android.widget.EditText; 

public class FormaLogin extends Dialog 
{ 
    SisLec sisLec; 

    public FormaLogin(SisLec _sisLec) 
    {  
     super(_sisLec);  
     sisLec = _sisLec;  
     setTitle("Identificación de Lecturista"); 
    } 

    public void mostrar() 
    { 
     setContentView(R.layout.login); 
     show(); 
    } 

    public void ingresarBtnClick(View view) 
    { 
     EditText numLecTxt = (EditText) sisLec.findViewById(R.id.numLecEditText); 
     EditText pinTxt = (EditText) sisLec.findViewById(R.id.pinEditText); 

     if(numLecTxt.getText().length() > 0) 
     { 
      if(pinTxt.getText().length() > 0) 
      { 
       if(numLecTxt.getText().equals("1337")) 
       { 
        if(pinTxt.getText().equals("8383")) 
        { 
         //sisLec.frmMantenimiento.mostrar(); 
        } 
       } 
       else 
       { 
        HiloIdentificacion hiloIden = new HiloIdentificacion(); 
        hiloIden.identificacion(numLecTxt.getText().toString(), pinTxt.getText().toString()); 
       } 
      } 
      else 
       sisLec.mensaje("Debe de ingresar su pin"); 
     } 
     else 
      sisLec.mensaje("Debe de ingresar su número de Lecturista"); 
    } 

    public void salirBtnClick(View view) 
    { 
     sisLec.salir(); 
    } 

    public void opcionesBtnClick(View view) 
    { 
     // TODO: Agregar método que muestre la forma de Opciones 
    } 

    private class HiloIdentificacion extends Thread 
    { 
     private String usuario, pass; 

     public synchronized void run() 
     { 
      try 
      { 
       sisLec.identificacion(usuario, pass); 
      } 
      catch(Exception e) 
      { 
       // TODO: Agregar registro de error 
      }     
     } 

     public synchronized void identificacion(String _usuario, String _pass) 
     { 
      usuario = _usuario; 
      pass = _pass; 
      run(); 
     } 
    } 
} 

Phương pháp này được gán cho nút "ingresarButton", "ingresarBtnClick (Chế độ xem xem)" là ở đó, như tài liệu Androir đề xuất http://developer.android.com/guide/topics/ui/ui-events.html nhưng tôi vẫn đang gặp lỗi.

Hiện có bất kỳ điều gì để làm điều đó tôi đang hiển thị bố cục này trên Hộp thoại không?

SisLec là lớp Hoạt động của tôi

Trả lời

10

android: onClick trong xml định tuyến sự kiện cho phương thức trong hoạt động. Nhưng phương pháp của bạn không có trong hoạt động, nó nằm trong lớp Dialog của bạn. Bạn sẽ cần phải có hoạt động của bạn chuyển tiếp cuộc gọi đến thể hiện của Hộp thoại, hoặc có mã hộp thoại tự đăng ký như trình nghe onClick thay vì cố gắng đặt nó trong bố cục.

+0

Và tôi muốn có lớp hoạt động của tôi trong sạch có thể, có mỗi hình thức/nút hộp thoại onlick phương pháp trên lớp của các hình thức/hộp thoại và didnt't muốn sử dụng OnClickListener thấy cách dễ dàng là để thiết lập các phương pháp Click cảm ơn cho câu trả lời, không thể tìm thấy bất cứ ai đã nêu – CJLopez

+0

Trên thực tế, nó định tuyến cuộc gọi hàm tới basecontext gắn liền với layoutinflater tạo ra nó, hoặc là cách khó để nói vấn đề là gì khi nó được viết trong 4 la khác nhau người ngu. –

3

Phương thức onClick cần nằm trong lớp Hoạt động.

+1

Cảm ơn, tôi không muốn cuốn lại lớp Hoạt động với mỗi phương pháp nút, có vẻ như tôi sẽ được thiết lập OnClickLsitener trên mỗi nút – CJLopez

Các vấn đề liên quan