Android 按钮无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10107337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button cannot be resolved to a type
提问by JTH
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProjectActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("android.intent.action.TUTORIALONE"));
}
});
}
}
Followed a youtube tutorial but i am Getting an error in this line:
遵循 youtube 教程,但我在这一行中遇到错误:
setContentView(R.layout.main);
"Button cannot be resolved to a type"
“按钮无法解析为类型”
I'm new to android dev so im confused. Help!
我是 android 开发新手,所以我很困惑。帮助!
采纳答案by Jviaches
use this in your xml :
在您的 xml 中使用它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/tutorial1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" >
</Button>
</LinearLayout>
回答by user3380020
move your cursor to "button" in your code and select "Import button(android.widget)"..
将光标移动到代码中的“按钮”并选择“导入按钮(android.widget)”..
回答by AJ.P
Try below steps: 1. Remove the import android.R (may have been accidentally imported) 2. clean the project 3. build the project again
尝试以下步骤: 1. 删除导入 android.R(可能被意外导入) 2. 清理项目 3. 再次构建项目
回答by Sambuca
Do you have a Button definied in you main.xml layout? Something like:
您是否在 main.xml 布局中定义了 Button?就像是:
<Button android:text="Click me" android:id="@+id/tutorial1"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
回答by Muhammad Nabeel Arif
Had you added follwoing in your root layout:
您是否在根布局中添加了以下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" />
回答by aisha gul ahmed
Move your cursor to "button" in your code and press ALT + Enter
将光标移动到代码中的“按钮”,然后按 ALT + Enter
I bold at which you have to press alt + enter
我大胆你必须按 alt + enter
Button tut1 = **(Button)** findViewById(R.id.tutorial1);