【问题标题】:How to can I make certain fields appear in R file?如何使某些字段出现在 R 文件中?
【发布时间】:2016-03-07 20:57:00
【问题描述】:

我有这些代码:

btnStart = (Button) findViewById(R.id.buttonstart);
btnStop = (Button) findViewById(R.id.buttonstop);
ipfield = (EditText) findViewById(R.id.ipfield);
portfield = (EditText) findViewById(R.id.portfield);

addPreferencesFromResource(R.xml.preferences);

它给了我这些错误:

buttonstart 无法解析或不是字段

buttonstop 无法解析或不是字段

ipfield 无法解析或不是字段

portfield 无法解析或不是字段

xml无法解析或不是字段

所以我进入 R 看看能否找到它们,但它们不存在。我尝试手动设置它们,但仍然出现错误。我多次清理项目,R 仍然没有生成它们。

这些是我的导入:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

Button btnStart, btnStop;
EditText ipfield, portfield;

【问题讨论】:

  • 你为什么要做addPreferencesFromResource(R.xml.preferences);这是一个设置活动?
  • 您做错了事并提出问题以不包括 R.Java 文件中的某些字段。请正确提问。
  • 您的 xml 文件可能有错误。这就是 R 文件没有生成的原因。我的个人建议 - 请跳进 Android 工作室。参考这个 - stackoverflow.com/a/30284292/3879847

标签: android


【解决方案1】:

我认为您没有在定义按钮和 EditTexts 的 xml 文件中声明 ID。 你必须用(例如)声明它:

android:id="@+id/buttonstart"

【讨论】:

  • 这就是问题所在。非常感谢!
【解决方案2】:

尝试使用 [您的包名].R 来引用您的视图,同时确保您的 xml 文件具有像这样设置的 id 属性;

android:id="@+id/buttonstart"

等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多