2011-04-19 68 views
1

好吧,我正在开发一个项目,我试图在活动中使用带有文本的按钮,但是eclipse无法找到id或chooser.xml找不到Id(资源问题)

这里的myActivity类

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> 

回答

1

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

+0

干净帮助清理了很多。 虽然仍然无法找到chooser.xml 它在 -res -layouts -chooser.xml – 2011-04-19 18:04:54

2

您的gen文件很可能还没有生成。尝试建设它,它应该使线路消失。

+0

加一,这是这个错误在我的处境修复。 – 2014-12-21 19:53:58

2

1)您需要导入您的R类。 import <android package>.R编号:R.id cannot be resolvedR cannot be resolved - Android error

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

+0

我回去并导入了.R包,它帮助清理了一些。我也修复了button1到button_1的问题,但这并没有帮助 – 2011-04-19 18:13:58

+0

,那么你现在得到了什么?告诉我们更新的错误状态。 – Priyank 2011-04-19 18:21:54

1

我知道这是一个很老的帖子,尽管我仍然在寻找类似问题时偶然发现了这个帖子。当我建立了Android项目,它会自动补充说:

import android.R; 

当我用上面的建议,

import <android package>.R; 

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

import android.R; 

并且没有错误。