Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 找不到Id(资源问题)_Android_Resources - Fatal编程技术网

Android 找不到Id(资源问题)

Android 找不到Id(资源问题),android,resources,Android,Resources,好的,我正在做一个项目,我试图在活动中使用按钮,按钮中包含文本,但是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;

好的,我正在做一个项目,我试图在活动中使用按钮,按钮中包含文本,但是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);
             }
         });
             }

    }
这是chooser.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>  

测试应用程序
test.png
按钮1
按钮2
按钮3
按钮4

chooser.xml是否位于正确的文件夹(项目提供的默认布局文件夹)中?你有没有试过去Project并选择Clean?代码中的一切看起来都正常。

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

您的gen文件很可能尚未生成。尝试构建它,它会使行消失。

您的gen文件很可能尚未生成。试着构建它,它会让行消失。

1)你需要导入你的R类<代码>导入和删除

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

1)你需要导入你的R类<代码>导入和删除


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

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

import android.R;
当我使用上述建议时

import <android package>.R;

没有错误。

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

import android.R;
当我使用上述建议时

import <android package>.R;

而且没有错误。

Clean有助于清除很多错误。尽管它仍然找不到chooser.xml,但它在-res-layouts中-chooser.xmlClean帮助清理了很多。尽管它仍然找不到chooser.xml,但它位于-res-layouts-chooser.xmlI返回并导入了.R包,它帮助清理了一些文件。我还将button1固定到button_1,但这没有帮助。我回去导入了.R包,它帮助清理了一些。我还将button1修改为button_1,但这没有帮助。加上一,这是在我的情况下修复此错误的方法。加上一,这是在我的情况下修复此错误的方法。