R.layout.main& R.id.gridview

时间:2011-04-02 00:59:01

标签: java android android-layout

尽管"导入android.R;"我仍然会收到上述错误。或者"导入my.package.name.R"

我在这里关注网格视图的Android教程:http://developer.android.com/resources/tutorials/views/hello-gridview.html

我的代码:

package com.examp.yoGridView;


import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CreateActivityBoxChecked extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //<<<<< ECLIPSE ERROR HERE

        GridView gridview = (GridView) findViewById(R.id.gridview); //<<<<< ECLIPSE ERROR HERE
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(CreateActivityBoxChecked.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我也使用了&#34; CTRL + SHIFT + O&#34;组织任何缺少的进口,但没有运气。清洁也没有区别。

您的建议?

Eclipse控制台上显示的错误消息:

Errors (2 items)
 R.id.gridview cannot be resolved
 R.layout.main cannot be resolved

2 个答案:

答案 0 :(得分:2)

我在课堂上经常看到这个问题。如果你删除“import android.R”,你的程序应该工作。问题是Eclipse正在寻找Android.R.layout.main而不是com.example.yoGridView.R.layout.main。

答案 1 :(得分:0)

如果您的xml有错误,则不会生成R.java。