Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

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

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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...