OnClickListener in Android Studio

前端 未结 5 1993
栀梦
栀梦 2020-12-09 10:46

I\'m attempting to develop and app using the new Android Studio, but I keep receiving major errors on my OnClickListeners. Mainly it is telling me that it cannot resolve sym

5条回答
  •  感动是毒
    2020-12-09 11:40

    you will need to button initilzation inside method instead of trying to initlzing View's at class level do it as:

     Button button;  //<< declare here..
    
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button= (Button) findViewById(R.id.standingsButton); //<< initialize here
             // set OnClickListener for Button here
            button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,StandingsActivity.class));
            }
          });
        }
    

提交回复
热议问题