2

I am trying to follow a manual (which had some bugs and I fixed it) however I am receiving this error btstart cannot be resolved or is not a field and I don't know how to fix it. Here's the LINK to that manual (but I have done changes in layout as well as string xml files) Here is the MainActivity.java code :

 package com.example.stopwatch;
//import com.example.stopwatch.R;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class MainActivity extends Activity implements OnClickListener {
    private Button start;
    private Button stop;
    private Button reset;

    private Chronometer mydChronometer;

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

    public void uI() {
        start = (Button) findViewById(R.id.btstart);
        start.setOnClickListener(this);
        stop = (Button) findViewById(R.id.btstop);
        stop.setOnClickListener(this);
        reset = (Button) findViewById(R.id.btreset);
        reset.setOnClickListener(this);
        mydChronometer = (Chronometer) findViewById(R.id.chronometer1);

    }

    @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;
    }

    @Override
    public void onClick(View v) {
        if (v == start) {
            mydChronometer.start();

        } else if (v == stop) {

            mydChronometer.stop();
        } else if (v== reset) {
            mydChronometer.setBase(SystemClock.elapsedRealtime());

        }

    }

}

Here comes my "activity_main.xml" :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chronometer1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:src="@drawable/myd" />

    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:gravity="center"
        android:text="Chronometer"
        android:textSize="35dp" />

    <Button
        android:id="@+id/btstart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/chronometer1"
        android:layout_marginTop="18dp"
        android:text="@string/start" />

    <Button
        android:id="@+id/btreset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btstop"
        android:layout_alignBottom="@+id/btstop"
        android:layout_toRightOf="@+id/btstop"
        android:text="@string/reset" />

    <Button
        android:id="@+id/btstop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btstart"
        android:layout_alignBottom="@+id/btstart"
        android:layout_toRightOf="@+id/btstart"
        android:text="@string/stop" />

</RelativeLayout>

And here's the "strings.xml" file:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">stopwatch</string>
    <string name="btn1">Start</string>
    <string name="btn2">Stop</string>
    <string name="btn3">Reset</string>
    <string name="menu_settings">Settings</string>
    <string name="action_settings">Settings</string>

</resources>

I haven't done any change in main.xml and here is my main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

Could you please let me know what am I missing and what's the fix to this error?

P.S.: I am receiving similar error for these lines:

btstop cannot be resolved or is not a field in stop = (Button) findViewById(R.id.btstop);
btreset cannot be resolved or is not a field in reset = (Button) findViewById(R.id.btreset);
chronometer1 cannot be resolved or is not a field in mydChronometer = (Chronometer) findViewById(R.id.chronometer1);

Thanks a ton.

jww
  • 97,681
  • 90
  • 411
  • 885
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • Clean and rebuild your app. – Sotirios Delimanolis Sep 10 '13 at 00:46
  • @SotiriosDelimanolis I cleaned the project now and (Ctrl+B) (build all) is inactive and when I press it nothing happens! And I have this new error `R cannot be resolved to a variable` added in this line `setContentView(R.layout.activity_main);` Any idea how to resolve these errors? – Mona Jalal Sep 10 '13 at 01:05

3 Answers3

3

Like Sotirios said, try clean the project, then in Project => uncheck clean automatically, then now you can build your project individually.

user1509803
  • 186
  • 1
  • 9
  • thanks for your hint! I was able to build it manually with your hint after I cleaned the project! That all being said yet I have the problem I mentioned `activity_main cannot be resolved or is not a field` in this line `setContentView(R.layout.activity_main);` I have also used "Shift+Ctrl+O" to include all the necessary libraries. I am really clueless what's wrong as I have checked all the possible solution in other Stackoverflow links.thanks! – Mona Jalal Sep 10 '13 at 02:14
  • Reset Stop so basically there is no such string name="stop" so your main.xml can't be compiled. – user1509803 Sep 10 '13 at 02:22
  • I suggest you double check all resources like string, drawable, make sure you've included their path correctly. – user1509803 Sep 10 '13 at 02:23
0

See this "id cannot be resolved or is not a field" error? it may help you.

Look if you re importing android .R

That could be the error

Community
  • 1
  • 1
Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26
0

R not being resolved is a result of the R.java file not being generated automatically.

Check for errors in your resources folder, as this is what the R.java automatic generation is based on. In my case, in my AndroidManifest.xml file, I had to change one of the lines from

<application android:icon="@drawable/*icon"* android:label="@string/app_name">

to

<application android:icon="@drawable/*ic_launcher"* android:label="@string/app_name">

(i.e. change the @drawable/icon to the name of the .png file in the resources (res) folder

Shiva
  • 6,677
  • 4
  • 36
  • 61
Ivan_ug
  • 2,467
  • 2
  • 17
  • 18