-2

I just started coding in android and i am stuck at this point my code is simple its showing error in only these lines. in the line setContentView(R.layout.activity_starting_point); its showing error that activity_starting_point can not be resolved or is not a field. why so please help!

package com.thenewboston.abhi;

import android.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class StartingPoint extends ActionBarActivity {

       int counter;
       Button add, sub;
       TextView display;

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

        counter=0;
        add = (Button)findViewById(R.id.button1);
        sub = (Button)findViewById(R.id.button2);
        display = (TextView)findViewById(R.id.text1);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Your total is " + counter);

            }
        });
        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("Your total is " + counter);

            }
        });

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.starting_point, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_starting_point, container, false);
            return rootView;
        }
    }

}

2 Answers2

0

Delete import android.R; as that is the Android API's R and not your project's one and is masking yours. You need your one as it contains your resources.

Try and see if auto-complete can find the target layout too. Type setContentView(R.layout. and see what options you are given. (You may need to invoke the auto-complete. It's Ctrl + Space on Eclipse. I don't know the shortcuts for Studio.)

Whenever you want to access the Android API's R.java you should fully qualify it with android.R.layout.whatever so it doesn't get auto-imported again and mess with your local resources.

If this doesn't work can you confirm that you have an R.java under the /gen/your/package/path folder? Perhaps try refreshing and rebuilding the project too.

If there are errors in your project R may not be able to successfully build. You should check your lint errors or other error logs and again refresh, rebuild, restart after sorting those.
Note that errors in your XML may not be picked up by eclipse so you should pay careful attention to those files. Just because the XML syntax is ok does not mean it is valid Android XML.

indivisible
  • 4,892
  • 4
  • 31
  • 50
  • now its saying R cannot be resolved to a variable. :( – user3580024 May 01 '14 at 19:40
  • Refresh and rebuild your project. Restarting eclipse can help too – indivisible May 01 '14 at 19:41
  • @user3580024 I've added more info to my answer – indivisible May 01 '14 at 19:46
  • gen folder is empty.i am using eclipse – user3580024 May 01 '14 at 19:47
  • And are there any errors being shown? Either in the project tree (left side as red 'x's on the project and sub folders leading to the problems) or in the lint warnings view (if not displayed go to: Toolbar > Window > Show view > Other > Android > Lint Warnings) – indivisible May 01 '14 at 19:56
  • tried that...still same error? – user3580024 May 01 '14 at 19:57
  • Make 1000% sure (not a typo) that there are no errors in your xml files. Any problem there and R will not build. Eclipse does not recognise all possible issues automatically. Try to delete (backup somewhere) your current layouts (and other xml files) and make a clean, empty new one. See if you can reference that (again - refresh, rebuild, restart in case). If it works then add your xml back in file by file to see which one's the problematic one. – indivisible May 01 '14 at 20:00
  • Is that after removing your custom xml or before? Eclipse will not identify all invalid xml so it may not show all actual errors. – indivisible May 01 '14 at 20:05
  • even if i am creating new android application project and running it its showing errors. there could be a possibility that i haven't installed eclipse correctly?? – user3580024 May 01 '14 at 20:12
  • in that path Toolbar > Window > Show view > Other > Android > Lint Warnings it was showing 0 warnings 0 errors – user3580024 May 01 '14 at 20:14
  • i created a new project did not even touch the xml file still its showing the same error R can not be resolved to a variable. it should be simply printing the hello_world string instead. – user3580024 May 01 '14 at 20:21
  • thanks bro for helping. just help me with why i am getting error when i am running the newly created untouched project? the error is same R can not be resolved to a variable. – user3580024 May 01 '14 at 20:35
  • If that is the case then this is a duplicate question. There is lots of material on this error to be found with simple searches. Here is the best of them: http://stackoverflow.com/a/7824975/1590950 – indivisible May 01 '14 at 20:37
0

check your build target, delete import Android.R, clear your project, and check the error log for more information about what is causing your app to not generate the R class

Kosh
  • 6,140
  • 3
  • 36
  • 67