当前位置: 代码迷 >> java >> 为什么我的Android应用程序找不到XML文件中声明的“文本视图”或“按钮”?
  详细解决方案

为什么我的Android应用程序找不到XML文件中声明的“文本视图”或“按钮”?

热度:23   发布时间:2023-07-17 20:30:20.0

我一直在关注New Boston使用Eclipse进行的有关Android编程的在线教程,但我陷入了一个特定的问题。 因此,一切正常,我的GUI界面中有一个按钮和一个TextView。 现在,当我转到Java文件并尝试使用R.Id .....来定位它时,它无法识别XML文件中声明的Button id或TextView Id。 为什么会出现此错误? 有人可以纠正我的错误吗? 我正在使用最低SDK-API8- Android 2.2,目标SDK-API 13-Android 3.2和使用SDK-API 19-Android 4.4进行编译。

我检查了以下有关Stack Overflow的文章 。 根据解决方案,我应该从我的java文件中删除导入的android.R,而我根本没有该导入。 所以,我有点困惑。

主要活动

package com.example.androidprogram1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    TextView display;
    Button show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = (Button)findViewById(R.id.button1);
        display = (TextView)findViewById(R.id.textView1);

//错误消息:-在ID类型(建议)中创建字段Button1 /在ID类型中创建常量TextView1 .....这是我得到的错误。 似乎按钮和textView的ID未添加到R.java文件中

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tvView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidprogram1.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="65dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/hello_world" />

</RelativeLayout>

非常感谢。!!

您需要实例化TextViewButton

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    show = (Button) findViewById(R.id.button1);
    display = (TextView) findViewById(R.id.textView1);
}

编辑:您的错误消息似乎是Eclipse问题,而不是您已经完成的事情,除非您进入并更改了R.java ,否则不应这样做。 保存您的项目,关闭它,然后重新打开并重新构建它。 另外,如果您碰巧在类中import android.R ,请将其删除。

  相关解决方案