5

作为 Android 应用程序的开发人员,我是新手。我发现一个问题。即 R.layout.main 无法解决。我该如何解决我的问题。我的代码在这里。请解决我的问题。

 package com.android;

import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */

    Button bt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.ButtonOk);
        bt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //Toast.makeText(getBaseContext(), "Welcome Android World", 3000).show();
            Intent intent = new Intent(HelloActivity.this, DisplayActivity.class);
            startActivity(intent);
            }
        });
    }
}




*

我的 xml 代码在这里:

*

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" android:id="@+id/ButtonOk" android:height="50dp" android:width="100dp"></Button>
</LinearLayout>
4

6 回答 6

11

第 1 步:移除import android.R;

第2步:(Clean And Rebuild它应该工作)

如果不

然后close the project退出eclipse并再次打开。按照它应该工作的步骤。

如果不

更改您的包裹

import android.R;

import yourpackage.R;

它应该工作

于 2013-01-07T16:56:19.120 回答
7

从标题中删除行 import android.R 。

并执行 Ctrl + Shift + O

如果再次出现android.R,则手动写入

import <yourpackagename>.R

看起来您使用的包名称为 com.android.R。理想情况下,避免使用像 com.android 这样的包名。尝试像 com.companyname.appname 一样维护它

因此,您养成了这种习惯,并且一旦您即将发布,就不必为更改所有文件夹中的包名称而感到痛苦。

于 2013-01-07T16:51:10.360 回答
1

导入 com.android.R 而不是 android.R 因为你的包名是 com.android

于 2013-01-07T16:47:34.490 回答
0

尝试清理并重建您的应用程序,这应该可以解决问题。

于 2013-01-07T16:46:55.147 回答
0

我的朋友,请检查您的 xml 文件的名称是否为“main”,因为我们总是写 setContentView(R.layout.your_xml_file_name); 如果您觉得我的解决方案值得,请喜欢它

于 2015-06-05T03:40:33.513 回答
0

我在 IntelliJ IDEA 上也遇到了这个问题,这就是我解决问题的方法,但首先我注意到以下内容:

  1. 这个问题并没有阻碍应用程序的执行
  2. 位于out/production/my/package/name目录下的资源文件(R.java)会自动更新,但位于gen/my.package.name目录下的资源文件(R.java)不会自动更新
  3. IntelliJ IDEA 使用未更新的 R.java 进行智能

因此,我将 R.java 文件中生成的布局类复制到out/production/my/package/name目录中,如下所示

public static final class layout {
    public static final int main = 2130903040;

    public layout() {
    }
}

我将它粘贴到gen/my.package.name目录中的 R.java 文件中

注意: 请不要修改out/production/my/package/name中自动生成的资源文件R.java文件

于 2015-11-13T14:58:45.757 回答