How to solve error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick

后端 未结 5 1256
太阳男子
太阳男子 2020-12-20 19:27

I have seen that there\'s been some similar questions but the answers to those haven\'t helped me so far. The full error:

java.lang.IllegalStateExcept

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 19:50

    Please rename onClick method to anything other than that, Android thinks you are calling the internal onClick from View.java exposed via OnClickListener interface

    and in your Activity

    public class StartActivity extends AppCompatActivity {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_start);
            }
    
            public void myOnClick(View v) {
              Log.d("DEBUG", "CLICKED " + v.getId());
            }
    }
    

    You can check the View documentation here

提交回复
热议问题