0

这里是编码

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


   }

该应用程序可以正常启动,但是单击该按钮时,会显示以下错误。

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'
4

3 回答 3

0

使用代码扩展 Samir 的答案(将 更改android:onClick="onCreate"android:onClick="btnclick"),向您展示如何使其工作:

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"));            
          }     
    }   
}
于 2012-06-21T14:16:34.407 回答
0

当您android:onClick="onCreate"在按钮中使用时,要处理按钮单击事件,您必须onCreate(View v)在您的活动中创建函数 a

 Button
        android:id="@+id/conv"
于 2012-06-21T10:43:11.600 回答
0

当我查看上面的代码时,按钮没有点击处理程序:)

尝试这个:

 <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"/>

并在您的 java 代码中创建一个方法 click_view,如下所示:

public void click_view(View v){
  //your to-do code here...
}
于 2012-06-21T10:47:26.603 回答