尝试在 android studio 中安装按钮时出现编译器错误
Compiler Errors when trying to install a button in android studio
我是 android 工作室的新手,刚刚开始开发我的第一个应用程序,
我之前设法编译了该应用程序,但刚刚尝试将代码添加到按钮以在活动之间进行更改,我环顾四周但无法弄清楚原因,但是当我尝试 运行 时出现 8 个编译错误但不知道为什么!
这是我的代码:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btnEnter"
android:layout_gravity="center_horizontal"
android:layout_weight="0.40"
android:textSize="50dp"
android:onClick="onClick"/>
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
下面是抛出编译器错误的代码:
public class MainActivity 扩展了 ActionBarActivity {
public 最终静态字符串 EXTRA_MESSAGE = "com.example.ben.learning.MESSAGE";
@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);
}
Button btn = (Button)findViewById(R.id.btnEnter);
btn.btnEnter.setOnClickListener(新View.OnClickListener()
{
public void onClick (View view){
Intent intent = new Intent
(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(intent);
}
);
}
}
...还有错误:
错误:(60、36)错误:预期
Error:(60, 37) 错误:类型的非法开始
错误:(60, 40) 错误:')' 预期
错误:(60, 45) 错误:';'预期的
Error:(60, 46) 错误:方法声明无效; return 需要类型
Error:(64, 9) 错误:表达式的非法开始
Error:(64, 16) 错误:表达式的非法开始
错误:(64, 35) 错误:';'预期的
Error:(70, 6) 错误:非法开始类型
任何见解都会很棒,我只是绕着圈子想弄明白!
干杯。 :)
我把按钮代码的位置改到了onCreate方法来解决错误
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnEnter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent
(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(intent);
}
});
}
我是 android 工作室的新手,刚刚开始开发我的第一个应用程序, 我之前设法编译了该应用程序,但刚刚尝试将代码添加到按钮以在活动之间进行更改,我环顾四周但无法弄清楚原因,但是当我尝试 运行 时出现 8 个编译错误但不知道为什么!
这是我的代码:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btnEnter"
android:layout_gravity="center_horizontal"
android:layout_weight="0.40"
android:textSize="50dp"
android:onClick="onClick"/>
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
下面是抛出编译器错误的代码:
public class MainActivity 扩展了 ActionBarActivity { public 最终静态字符串 EXTRA_MESSAGE = "com.example.ben.learning.MESSAGE";
@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);
}
Button btn = (Button)findViewById(R.id.btnEnter); btn.btnEnter.setOnClickListener(新View.OnClickListener()
{
public void onClick (View view){
Intent intent = new Intent
(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(intent);
}
);
}
} ...还有错误:
错误:(60、36)错误:预期 Error:(60, 37) 错误:类型的非法开始 错误:(60, 40) 错误:')' 预期 错误:(60, 45) 错误:';'预期的 Error:(60, 46) 错误:方法声明无效; return 需要类型 Error:(64, 9) 错误:表达式的非法开始 Error:(64, 16) 错误:表达式的非法开始 错误:(64, 35) 错误:';'预期的 Error:(70, 6) 错误:非法开始类型
任何见解都会很棒,我只是绕着圈子想弄明白!
干杯。 :)
我把按钮代码的位置改到了onCreate方法来解决错误
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnEnter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent
(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(intent);
}
});
}