按钮无法解析为一种类型

3
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"));

            }
        });

    }
}

我正在遵循 YouTube 上的教程,但这一行出现了错误:
setContentView(R.layout.main);

"Button cannot be resolved to a type"(无法将Button解析成类型)

我是Android开发新手,所以感到困惑。求助!


如果你在Eclipse中工作,ctrl+shift+o可以解决缺少或者未使用的导入。尽管你正在导入Button,所以你收到这个消息很奇怪。 - Jave
是的,@Muhammad Nabeel Arif的回答很重要。 - Praveenkumar
执行项目清理 - Proxy32
6个回答

1
将光标移动到你代码里的"button",选择"导入按钮(android.widget)"。

0
你的main.xml布局中是否定义了一个Button? 类似这样的东西:
<Button android:text="Click me" android:id="@+id/tutorial1"
    android:layout_width="wrap_content" android:layout_height="wrap_content"/>

我有这个: <Button android:id="@+id/tutorial1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮"/> - JTH

0

如果您在根布局中添加了以下内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" />

<?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" >

0

在你的 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>

0
请尝试以下步骤: 1. 删除导入的android.R(可能是意外导入的) 2. 清理项目 3. 再次构建项目

-1
将光标移动到代码中的“button”上,然后按下ALT + Enter
我将加粗需要按下alt + enter的部分。
Button tut1 = **(Button)** findViewById(R.id.tutorial1);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接