加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

无法从Java程序中引用的main.xml获取小部件

发布时间:2020-12-15 08:35:28 所属栏目:Java 来源:网络整理
导读:制作了一个非常基本的xml文件来尝试访问一个简单的按钮小部件. main.xml文件是: LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fi
制作了一个非常基本的xml文件来尝试访问一个简单的按钮小部件. main.xml文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@color/white">
  <TextView android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"/>
  <TextView android:text="Heading Text" 
            android:id="@+id/TextView01" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
  </TextView>
  <Button android:text="Button Text" 
          android:id="@+id/Button01" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content">
  </Button>
</LinearLayout>

Java程序是

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;


public class TestButton extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button myButton = (Button)findViewById(android.R.id.Button01);

    }
}

“Button”行上的错误是:
Button01无法解析或不是字段

任何想法,我做的是什么非常基本的错误:( :(.

解决方法

Button myButton = (Button)findViewById(android.R.id.Button01);

这是错的.它不是从您的项目引用自动生成的R.java,而是引用标准的android.R类.它应该是:

Button myButton = (Button) findViewById(R.id.Button01);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读