【问题标题】:Id cannot be found (Resource Issue)找不到 ID(资源问题)
【发布时间】:2011-04-19 17:51:43
【问题描述】:

好的,所以我正在处理一个项目,我正在尝试在活动中使用带有文本的按钮,但是 eclipse 找不到 id 或 chooser.xml

这是我的活动类

package com.xxxxxxx;

import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;   
import android.widget.Button;  
import android.widget.ImageButton;  
import android.view.*;

public class MyActivity extends Activity {`

    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;


        protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
                //chooser is underlined (chooser cannot be resolved or is not a field)
        setContentView(R.layout.chooser);
                //all ids are underlined(id cannot be resolved or is not a field)
        mButton1 = (Button) findViewById(R.id.button1);
        mButton2 = (Button) findViewById(R.id.button2);
        mButton3 = (Button) findViewById(R.id.button3);
        mButton4 = (Button) findViewById(R.id.button4);

         mButton1.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent myIntent = new Intent(MyActivity.this, Activity2.class);
                 startActivity(myIntent);
             }
         });
         mButton2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent myIntent = new Intent(MyActivity.this, Activty3.class);
                 startActivity(myIntent);
             }
         });

         mButton3.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent myIntent = new Intent(MyActivity.this, Activity4.class);
                 startActivity(myIntent);
             }
         });
         mButton4.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent myIntent = new Intent(MyActivity.this, Activity5.class);
                 startActivity(myIntent);
             }
         });
             }

    }

这是选择器.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">  

 <Button
     android:id="@+id/button_1"  
     android:layout_height="wrap_content"  
     android:layout_width="wrap_content"  
     android:text="@string/button_1" /> 

   <Button 
   android:text="@string/button_2"   
   android:id="@+id/button_2"   
   android:layout_width="wrap_content"   
   android:layout_height="wrap_content"/>  

   <Button 
   android:id="@+id/button_3"   
   android:layout_width="wrap_content"   
   android:layout_height="wrap_content"  
   android:text="@string/button_3"   
  />

  <Button
     android:id="@+id/button_4"   
     android:layout_height="wrap_content"  
     android:layout_width="wrap_content"  
     android:text="@string/button_4"  
     />  

</LinearLayout>

这里是字符串资源 xml

 <?xml version="1.0" encoding="utf-8"?>  
<resources> 
    <string name="app_name">Test App</string>  
    <string name="test_image">test.png</string>   
    <string name="button_1">button_1</string>  
    <string name="button_2">button_2</string>  
    <string name="button_3">button_3</string>  
    <string name="button_4">button_4</string>  
</resources>  

【问题讨论】:

    标签: android resources


    【解决方案1】:

    您的 gen 文件很可能尚未生成。尝试构建它,它应该使线条消失。

    【讨论】:

    • 加一个,这是针对我的情况的此错误的修复。
    【解决方案2】:

    1) 您需要导入您的 R 类。 import &lt;android package&gt;.R 参考:R.id cannot be resolved AND R cannot be resolved - Android error

    2) 你的R.id.button1 不应该是R.id.button_1 等等其他按钮吗?

    【讨论】:

    • 我回去导入了 .R 包,它帮助清理了一些。我还将 button1 固定为 button_1,但这没有帮助
    【解决方案3】:

    chooser.xml 是否在正确的文件夹中(项目提供的默认布局文件夹)?您是否尝试过进入 Project 并选择 Clean?您的代码中的一切看起来都正常。

    【讨论】:

    • 清理帮助清理了很多东西。虽然它仍然找不到 chooser.xml 它在 -res -layouts -chooser.xml
    【解决方案4】:

    我知道这是一篇非常古老的帖子,尽管我在寻找类似问题时仍然偶然发现了它。在我构建android项目时,它自动添加了:

    import android.R;
    

    当我使用上面的建议时,

    import <android package>.R;
    

    我仍然使用 R.id .....,它仍然传播错误。我删除了

    import android.R;
    

    没有错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多