Middle Of Tech Ramblings of tech, software development, chocolate, marathons…

1Oct/1018

Android “main cannot be resolved or is not a field”

So I've been messing with Android development for awhile now and one of the noob errors I stumbled on awhile back was the compile error...

"main cannot be resolved or is not a field"

What this usually is caused by is letting Eclipse resolve the compile problem of "main" not being found. It resolves this by importing android.R. This is obviously not what we want.

To fix this simply change the import to be ...

import hello.World.R

or whatever your package is named. This will resolve the "main" problem. Hopefully this helps!

Filed under: Tech Leave a comment
Comments (18) Trackbacks (0)
  1. Thanks a lot! It’s really help.

  2. No problem! Good luck!

  3. Hey thanks!
    It works

  4. Great! Thanks

  5. Thanks a lot friend ………..

  6. Thanks a lot!!!
    It really help me resolve the problems!!!

  7. Thank you for providing really informative posts on your site. How can I share it on Facebook?

  8. Hey,Really it works.
    But import android.R; is not working why ??

  9. Im new to android. Maybe I’m doing something wrong, but it does not seem to be working for me… Below is my code. Kindly help…

    package my.hellospinner1;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Spinner;
    import android.widget.SpinnerAdapter;
    import android.widget.ArrayAdapter;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.SimpleAdapter;
    import android.view.View;
    import android.widget.Toast;
    import my.hellospinner1.*;

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

    Spinner spinner = (Spinner) findViewById(android.R.id.spinner);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(
    this, android.R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    }

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView parent,
    View view, int pos, long id) {
    Toast.makeText(parent.getContext(), “The planet is ” +
    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView parent) {
    // Do nothing.
    }
    }
    }

    Errors:

    1. Main cannot be resolved or is not a field
    2. spinner cannot be resolved or is not a field
    3. planets_array cannot be resolved or is not a field

  10. Aravind – Did you ever get your issues resolved? Typically when you get the “cannot be resolved” messages you’re missing an import or something isn’t being referenced correctly. Also – remember that Java is case sensitive.

  11. If you found any error in the R and main file i.e. setcontentview(R.layout.main);. You just install the android avd manager select the emulator then you will be freed by this defect.

  12. Thanks alot, It was indeed helpful

  13. Hi,

    I am new to android,I dont know how to recover my error,Kindly help……..

    package com.example.socialapp;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.Toast;
    import android.view.ActionMode;
    import android.view.View;

    public class OverviewActivity extends Activity {
    protected Object mActionMode;

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

    View view = findViewById(R.id.myView);
    view.setOnLongClickListener(new View.OnLongClickListener() {
    // Called when the user long-clicks on someView
    public boolean onLongClick(View view) {
    if (mActionMode != null) {
    return false;
    }

    // Start the CAB using the ActionMode.Callback defined above
    mActionMode = OverviewActivity.this
    .startActionMode(mActionModeCallback);
    view.setSelected(true);
    return true;
    }
    });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_overview, menu);
    return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(this, “Just a test”, Toast.LENGTH_SHORT).show();
    return true;
    }

    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate a menu resource providing context menu items
    MenuInflater inflater = mode.getMenuInflater();
    // Assumes that you have “contexual.xml” menu resources
    inflater.inflate(R.menu.contextual, menu);
    return true;
    }

    // Called each time the action mode is shown. Always called after
    // onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
    case R.id.toast:
    Toast.makeText(OverviewActivity.this, “Selected menu”,
    Toast.LENGTH_LONG).show();
    mode.finish(); // Action picked, so close the CAB
    return true;
    default:
    return false;
    }
    }

    // Called when the user exits the action mode
    public void onDestroyActionMode(ActionMode mode) {
    mActionMode = null;
    }
    };
    }

    Errors:
    Toast cannot be resolved or not a field.

  14. If you found any error in the R and main file i.e. setcontentview(R.layout.main);. You just install the android avd manager select the emulator then you will be freed by this defect.

  15. This did solve my own problem. I am new and very appreciative. But a number of other simple projects referred to R.layout.main and did not need to import R (of any stripe). I don’t understand why it would be needed all of a sudden–and it is not in the code example I am following. (Albeit the tutorial is HoneyComb, and I have to compile for Ice Cream Sandwich.

  16. Ok I’m still getting an error on R and this is what I have:
    package com.practice.myfirstapp;

    import com.practice.myfirstapp.*;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

  17. what does it mean when a song i want to play cannot be resolved to field
    is there some thing wrong with a xml file / i could place the code here @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MediaPlayer.thepoint= MediaPlayer.create(this,R.raw.thepoint);
    }
    }


Leave a comment

No trackbacks yet.

SEO Powered by Platinum SEO from Techblissonline