无法解析符号setOnClickListener& “;” &安培; “)”预期

时间:2016-02-16 20:56:50

标签: java android

我收到以下错误

  

DataContext

下面的Java代码。

我也收到错误
  

Cannot Resolve Symbol setOnClickListener

介于public void onClick(View v) {之间k预计会有(;w之间会有v。我不完全确定我是如何收到这些错误的,我们将不胜感激。

) & ;

下面是我的xml代码;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    //Creating button references for the buttons corresponding to their IDs through findViewById
    Button rB = (Button) findViewById(R.id.rButton);
    Button lB = (Button) findViewById(R.id.lButton);
    //Register click event with first button
    rB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView txtView = (TextView) findViewById(R.id.textView);//Correspond the text view to its ID
            txtView.setTextSize(14); //Change text size
        }
    });
    //Register click event with second button
    lB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView txtView = (TextView) findViewById(R.id.textView); //Correspond the text view to its ID
            txtView.setTextSize(24); //Change text size
        }
    });


}

2 个答案:

答案 0 :(得分:2)

您需要将代码放在onCreate()等方法体中。将它放在课堂体内并不具有语法上的有效性。

setOnClickListener()之后将调用onCreate()的代码移至setContentView()。您可以在类级别中使用变量声明(但仅在findViewById()之后使用setContentView()初始化它们。)

答案 1 :(得分:0)

移动以下代码:

//Creating button references for the buttons corresponding to their IDs through findViewById
    Button rB = (Button) findViewById(R.id.rButton);
    Button lB = (Button) findViewById(R.id.lButton);
    //Register click event with first button
    rB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView txtView = (TextView) findViewById(R.id.textView);//Correspond the text view to its ID
            txtView.setTextSize(14); //Change text size
        }
    });
    //Register click event with second button
    lB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView txtView = (TextView) findViewById(R.id.textView); //Correspond the text view to its ID
            txtView.setTextSize(24); //Change text size
        }
    });

在onCreate方法中,原因是因为那些语句必须在方法内,而不是任何方法,而是视图有效的方法(如onCreate)