java - 类型无法解析或不是字段

标签 java android xml

我刚刚开始构建 Android 应用程序。还有这个错误

button cannot be resolved to a type

总是让我烦恼。这是java代码

package com.android.revision1;
import android.app.Activity;
import android.os.Bundle;
public class Revision1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button=(Button) findViewByid(R.layout.button1);
}
}

这是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/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button" />

</LinearLayout>

最佳答案

button 似乎未声明,因此声明该字段

private Button button;

或局部变量:

Button button=(Button) findViewByid(R.layout.button1);

关于java - 类型无法解析或不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33660898/

相关文章:

java - Apache POI Word 使用自定义标题样式

java - 在 Java 中使用 RegEx 解析 CSV 输入

java - 正则表达式java从字符串中提取余额

java - 在android中排序 ListView

xml - 使用 XML/RCurl R 包解析 HTML 表,而不使用 readHTMLTable 函数

java - 单击 Enter 时如何解决扫描仪错误

android - 有没有办法在 native react 中运行前台服务?

android - 如何为 Android 游戏绘制可滚动的背景

java - XMLUnit 期望子节点数为真但为假

xml - 用 XML 表示图形