Java 无法解析符号setOnClickListener&&“&引用&&引用;)&引用;预期
我收到以下错误Java 无法解析符号setOnClickListener&&“&引用&&引用;)&引用;预期,java,android,Java,Android,我收到以下错误 无法解析符号集ClickListener 关于下面的Java代码。我还收到了一个关于 public void onClick(视图v){ 在k和之间(有一个;预期,在w和v之间有一个)&;预期。我不完全确定我是如何收到这些错误的,如果有任何帮助,我们将不胜感激 import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import and
无法解析符号集ClickListener
关于下面的Java代码。我还收到了一个关于
public void onClick(视图v){
在k
和之间(
有一个;
预期,在w
和v
之间有一个)&;
预期。我不完全确定我是如何收到这些错误的,如果有任何帮助,我们将不胜感激
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
}
});
}
下面是我的xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp" tools:context=".MainActivity">
<TextView android:text="Please Type Here" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:inputType="text"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#FF0000FF"
android:layout_below="@+id/editText"
android:layout_marginTop="40dp"
android:id="@+id/view" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LEFT"
android:id="@+id/lButton"
android:layout_alignTop="@+id/view"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RIGHT"
android:id="@+id/rButton"
android:layout_alignTop="@+id/view"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"/>
</RelativeLayout>
您需要将代码放在方法体中,例如
onCreate()
。将其放在类体中在语法上是无效的
将调用
setContentView()
的代码移动到setContentView()
之后的onCreate()
。您可以在类级别中使用变量声明(但只有在setContentView()
之后才使用findViewById()
初始化它们)移动以下代码:
//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)仅从错误消息中,可能在某个地方有一个输入错误…可能不在您提供的代码中。此代码具体在哪里?在
onCreate()等方法中
还是在类主体中?@laalto抱歉,我忘记了其余的Java代码,它在公共类main活动中
谢谢,这有助于我理解我做错了什么。