有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用Android文本视图时出错

我很确定switch语句的这一部分有问题,但我被难住了,我只需要有人检查一下

switch(buttonId)
        {
  /* LOTS OF CASES HERE */
  default:
  case R.id.goButton:
        TextView rootNum = (TextView)findViewById(R.id.otherView);
        String rootDone = rootNum.getText().toString();
        it = Integer.parseInt(rootDone);
      break;
  }

你能看出什么不对劲吗

更新

很抱歉没有发布日志,我认为问题很明显,但似乎不是这样

以下是两个logcat错误:

java.lang.IllegalStateException: Could not find a method RootMe(View) in the activity class root.me.RootMeActivity for onClick handler on view class 安卓.widget.Button with id 'goButton'

Caused by: java.lang.NoSuchMethodException: RootMe [class 安卓.view.View]

共 (2) 个答案

  1. # 1 楼答案

    问题是,在xml中,您提到了goButton的onclick事件来调用RootMe方法(android:onClick="RootMe"),但您尚未在活动中定义该方法。 所以从XML中删除android:onClick="RootMe",并将onClickListener分配给活动中的按钮

    示例代码

    XML

    <Button android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="Button1"
    android:id="@+id/btn1" />
    
    
    <Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button2"
    android:id="@+id/btn2" />
    

    源代码

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class ExampleActivity extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button btn1 = (Button) findViewById(R.id.btn1);
            btn1.setOnClickListener(this);
            Button btn2 = (Button) findViewById(R.id.btn2);
            btn2.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int id = arg0.getId();
            System.out.println("....clicked id..."+id);
            System.out.println("....id of btn1.."+R.id.btn1);
            System.out.println("....id of btn2.."+R.id.btn2);
            switch(id){
            case R.id.btn1:
                Toast.makeText(ExampleActivity.this, "....."+1, Toast.LENGTH_SHORT).show();
    
                break;
            case R.id.btn2:
                Toast.makeText(ExampleActivity.this, "....."+2, Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }
    
  2. # 2 楼答案

    在默认值后写关键字“break”