Using to spinners, app works fine, but when the button is clicked error is displayed

僤鯓⒐⒋嵵緔 提交于 2019-12-12 05:27:05

问题


here;s the coding

  <Button
        android:id="@+id/conv"
        android:layout_width="150dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="Convert" 
        android:onClick="onCreate"/>

public class LstActivity extends Activity {

EditText et;
TextView tv;
Button b1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState ) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et=(EditText)findViewById(R.id.editText1);

 Spinner spinner=(Spinner)findViewById(R.id.spinner1);
 Spinner s2=(Spinner)findViewById(R.id.spinner2);


 ArrayAdapter<?> adapter=ArrayAdapter.createFromResource(this, R.array.weight, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spinner.setAdapter(adapter);    
  spinner.setOnItemSelectedListener(new myaction());


  tv=(TextView)findViewById(R.id.textView3);

  ArrayAdapter<?> adapter1=ArrayAdapter.createFromResource(this, R.array.weight, android.R.layout.simple_spinner_item);
  adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  s2.setAdapter(adapter1);    
   s2.setOnItemSelectedListener(new myaction2());
   b1=(Button)findViewById(R.id.conv);
   et.setText("0");


   float x=(Float.parseFloat(String.valueOf(et.getText())));





   if ((spinner.getSelectedItemPosition()==2) && ((s2.getSelectedItemPosition()==1)))
   {

       tv.setText(String.valueOf(gmtomilli(x)+"mg"));
   }


   }

The app starts fine, but when the button is clicked, the following error is shown.

06-21 15:48:46.509: E/AndroidRuntime(2260): java.lang.IllegalStateException: Could not find a method onCreate(View) in the activity class kk.and.LstActivity for onClick handler on view class android.widget.Button with id 'conv'

回答1:


as you are using android:onClick="onCreate" in button so to handle the button click event you must create function a onCreate(View v) in your activity which has

 Button
        android:id="@+id/conv"



回答2:


As I looked at the code above, there is no click handler for the button :)

Try this:

 <Button
        android:id="@+id/conv"
        android:layout_width="150dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="Convert" 
        android:onClick="click_view"/>

And create a method click_view in your java code something like this:

public void click_view(View v){
  //your to-do code here...
}



回答3:


Expanding on Samir's answer (change the android:onClick="onCreate" to android:onClick="btnclick") with code showing you how to make it work:

public class LstActivity extends Activity {   

    EditText et;   
    TextView tv;   
    Button b1;   
    Spinner spinner;
    Spinner s2;
    float x;

    @Override  
    public void onCreate(Bundle savedInstanceState ) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        spinner=(Spinner)findViewById(R.id.spinner1);      
        s2=(Spinner)findViewById(R.id.spinner2);     

        // your other code

        x=(Float.parseFloat(String.valueOf(et.getText())));           

    } 

    public void btnclick(View v){       
          if ((spinner.getSelectedItemPosition()==2) && ((s2.getSelectedItemPosition()==1))) {            
            tv.setText(String.valueOf(gmtomilli(x)+"mg"));            
          }     
    }   
}


来源:https://stackoverflow.com/questions/11136383/using-to-spinners-app-works-fine-but-when-the-button-is-clicked-error-is-displ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!