1
package de.vogella.android.sqlite.first;

import java.util.List;
import java.util.Random;

import android.R;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class TestDatabaseActivity extends ListActivity {
  private CommentsDataSource datasource;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    datasource = new CommentsDataSource(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();

    // use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

  // Will be called via the onClick attribute
  // of the buttons in main.xml
  public void onClick(View view) {
    @SuppressWarnings("unchecked")
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    Comment comment = null;
    switch (view.getId()) {
    case R.id.add:
      String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
      int nextInt = new Random().nextInt(3);
      // save the new comment to the database
      comment = datasource.createComment(comments[nextInt]);
      adapter.add(comment);
      break;
    case R.id.delete:
      if (getListAdapter().getCount() > 0) {
        comment = (Comment) getListAdapter().getItem(0);
        datasource.deleteComment(comment);
        adapter.remove(comment);
      }
      break;
    }
    adapter.notifyDataSetChanged();
  }

  @Override
  protected void onResume() {
    datasource.open();
    super.onResume();
  }

  @Override
  protected void onPause() {
    datasource.close();
    super.onPause();
  }

} 

This is my code above.

The three errors that I am receiving are:

setContentView(R.layout.main); 
                         ^
                      main cannot be resolved or is not a field

&&&

case R.id.add:
           ^
        add cannot be resolved or is not a field

&&&

case R.id.delete:
           ^
        delete cannot be resolved or is not a field
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
Icy
  • 17
  • 4
  • Have you created the XML file? – Joel Fernandes Jan 02 '14 at 17:42
  • 2
    remove `import android.R;` – Shayan Pourvatan Jan 02 '14 at 17:43
  • If you're working in Eclipse, go to the "Problems" tab near the bottom and open it. Also look at your resources in the package explorer on the left. Chances are, you'll see that there was a build problem with your resources (a malformed xml file will do it). Any problems with your resources will cause 'R' to not be built, or to have problems. In short, it looks like res/layout/main is missing or couldn't be compiled, and "id" and "delete" ids are missing as well. – Edward Falk Jan 02 '14 at 17:44

4 Answers4

2

Remove the import to android.R

It is getting confused by the namespaces

MrZander
  • 3,031
  • 1
  • 26
  • 50
1

You have to remove
import android.R

Then clean and rebuild project. R will be generated.

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
1
import android.R; 

instead use

import yourpackgename.R;
Triode
  • 11,309
  • 2
  • 38
  • 48
1

first remove the import

import android.R;

and then go to line

setContentView(R.layout.main); 

and hover it and add the import of your main package and after then import android.R;

Sunny
  • 219
  • 1
  • 10