2013-10-06 5 views
1

У меня есть следующий код:Ошибка кнопки Android onClick?

в OnCreate метод:

{ 
saveClick = (Button) findViewById(R.id.saveBtn); 
     saveClick.setOnClickListener(this); 
} 

@Override 
    public void onClick(View v) { 
     //------------ take input data and save in DB -------------------- 
     String cpassword; 
     EditText num_1=(EditText)findViewById(R.id.num1); 
     EditText num_2=(EditText)findViewById(R.id.passkey1); 
     EditText num_3=(EditText)findViewById(R.id.passkey2); 
     number=num_1.getText().toString(); 
     password=num_2.getText().toString(); 
     cpassword=num_3.getText().toString(); 

     DBoperation db=DBoperation.getInstance(getApplicationContext()); 
     if(password == cpassword) 
     { 
      TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 
      pnumber = tMgr.getLine1Number(); 
      db.addSIMSs(pnumber); 
      db.addSIMSs(number); 
      db.addPassKey(password); 
     } 
    } 

Я использую затмение для развития, и продолжает давать следующие ошибки: в строке: saveClick.setOnClickListener (это);
Ошибка:
Метод setOnClickListener (View.setOnClickListener) в представлении типа не применим для аргументов.

также в строке: общественного недействительными OnClick (Просмотр объем) {
ошибка:
метод OnClick (Просмотр) типа MainActivity необходимо переопределить или реализовать метод супертипом.

Ниже приведен XML-файл:

<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: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=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="113dp" 
     android:text="TextView" /> 

    <EditText 
     android:id="@+id/num1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:layout_marginLeft="38dp" 
     android:layout_marginTop="37dp" 
     android:ems="10" 
     android:inputType="number" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/passkey1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/num1" 
     android:layout_below="@+id/num1" 
     android:layout_marginTop="86dp" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <EditText 
     android:id="@+id/passkey2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/passkey1" 
     android:layout_below="@+id/passkey1" 
     android:layout_marginTop="49dp" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <TextView 
     android:id="@+id/pass1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/passkey1" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_marginBottom="35dp" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/pass2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView3" 
     android:layout_alignTop="@+id/passkey2" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/pass1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="24dp" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <Button 
     android:id="@+id/saveBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/passkey2" 
     android:layout_marginLeft="14dp" 
     android:layout_marginTop="41dp" 
     android:text="@string/save" /> 

</RelativeLayout> 

ответ

4

внедривших вы OnClicklistener к вашей деятельности?

public class MainActivity extends Activity implements OnClickListener { 
0

В этом line.-

saveClick.setOnClickListener(this); 

Я предполагаю, что this является экземпляром Activity, когда он должен быть OnClickListener. Попробуйте что-то вроде этого instead.-

saveClick.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     // Do your stuff here  
    } 
}); 

Что касается второй ошибки, компилятор жалуется, что вы пытаетесь переопределить метод onClick в вашем Activity, но его родитель не реализует этот метод. Просто удалите метод onClick из Activity и переместите эту логику на приемник onClick.

0

Вы должны реализовать OnCLickListener в инициализации вашего класса. То есть public class MyClass extends Activity implements View.OnCLickListener

0

Используйте это, оно будет работать.

saveClick = (Button) findViewById(R.id.saveBtn); 
saveClick.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       String cpassword; 
     EditText num_1=(EditText)findViewById(R.id.num1); 
     EditText num_2=(EditText)findViewById(R.id.passkey1); 
     EditText num_3=(EditText)findViewById(R.id.passkey2); 
     number=num_1.getText().toString(); 
     password=num_2.getText().toString(); 
     cpassword=num_3.getText().toString(); 

     DBoperation db=DBoperation.getInstance(getApplicationContext()); 
     if(password == cpassword) 
     { 
      TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 
      pnumber = tMgr.getLine1Number(); 
      db.addSIMSs(pnumber); 
      db.addSIMSs(number); 
      db.addPassKey(password); 
     } 
    } 
}); 
0

Я думаю, что у вас может возникнуть проблема при импорте библиотеки onclicklistner; который, кажется, что вы сделали

public class MainActivity extends Activity implements OnClickListener 

почему бы вам не попробовать с

saveClick.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     // Do your stuff here  
    } 
}); 

, а затем

saveClick.setOnClickListener(this); 

, который поможет вам отладить futher

0

Добавьте это в OnClick

if (v.getId() == R.id.saveBtn) { 

    //your code 
     String cpassword; 
       EditText num_1=(EditText)findViewById(R.id.num1); 
       EditText num_2=(EditText)findViewById(R.id.passkey1); 
       EditText num_3=(EditText)findViewById(R.id.passkey2); 
       number=num_1.getText().toString(); 
       password=num_2.getText().toString(); 
       cpassword=num_3.getText().toString(); 

       DBoperation db=DBoperation.getInstance(getApplicationContext()); 
       if(password == cpassword) 
       { 
        TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 
        pnumber = tMgr.getLine1Number(); 
        db.addSIMSs(pnumber); 
        db.addSIMSs(number); 
        db.addPassKey(password); 
       } 

} 
Смежные вопросы