OnClickListener in Android Studio

前端 未结 5 1940
栀梦
栀梦 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));
            }
          });
        }
    
    0 讨论(0)
  • 2020-12-09 11:42
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_my);
        titolorecuperato = (TextView) findViewById(R.id.textView);
        String stitolo = titolorecuperato.getText().toString();
    
        Button btnHome = (Button) findViewById(R.id.button);
    
        btnHome.setOnClickListener(new View.OnClickListener() {
    
           @Override
            public void onClick(View view) {
    
           }
    });
    

    same thing as Nic007 said before.

    You do need to write code inside "onCreate" method. Sorry me too for the indent... (first comment here)

    0 讨论(0)
  • 2020-12-09 11:42
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
             int id = item.getItemId();
             if (id == R.id.standingsButton) {
                startActivity(new Intent(MainActivity.this,StandingsActivity.class));
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
    0 讨论(0)
  • 2020-12-09 11:47
    Button button= (Button) findViewById(R.id.standingsButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,StandingsActivity.class));
        }
    });
    

    This code is not in any method. If you want to use it, it must be within a method like OnCreate()

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button button= (Button) findViewById(R.id.standingsButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,StandingsActivity.class));
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-09 11:48

    This worked for me:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newarea);
    
        btnSave = (Button)findViewById(R.id.btnSave);
    
        OnClickListener btnListener = new OnClickListener() {
            @Override
            public void onClick(android.view.View view) {
                finish();
            }
        };
        btnSave.setOnClickListener(btnListener);
    
    }
    
    0 讨论(0)
提交回复
热议问题