Search code examples
androidstopwatchchronometer

Cannot resolve symbol issues in making a stopwatch


(Keep in mind i'm very new to coding) I'm having an issue when coding a stopwatch app, I've scavenged this site and found no solution to my issue, comparing my code to another user's who attempted the same tutorial, i found that they are the same, yet I am getting several unresolved symbols when to my knowledge, everything is correct...

Ok, i updated the file with some corrections, now im not having any more errors with start/stop, but i have new errors with the m(start/stop/etc)Listener code...

(Here's the Main Activity Java File)

package com.jackson.eason.stopwatch.;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import your.package.R;

public class MainActivity extends Activity {
    Chronometer mChronometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button button;

        mChronometer = (Chronometer) findViewById(R.id.Chronometer);

        // Watch for button clicks.
        button = (Button) findViewById(R.id.start);
        button.setOnClickListener(mStartListener);

        button = (Button) findViewById(R.id.stop);
        button.setOnClickListener(mStopListener);

        button = (Button) findViewById(R.id.reset);
        button.setOnClickListener(mResetListener);

        button = (Button) findViewById(R.id.set_format);
        button.setOnClickListener(mSetFormatListener);

        button = (Button) findViewById(R.id.clear_format);
        button.setOnClickListener(mClearFormatListener);

        View.OnClickListener mStartListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.start();
            }
        };

        View.OnClickListener mStopListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.stop();
            }
        };

        View.OnClickListener mResetListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setBase(SystemClock.elapsedRealtime());
            }
        };

        View.OnClickListener mClearFormatListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setFormat(null);
            }
        };
    }


    }

(Also, here's my xml file, which Android Developer says is fine)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Chronometer android:id="@+id/chronometer"
        android:format="@string/chronometer_initial_format"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0"
        android:paddingBottom="30dip"
        android:paddingTop="30dip"
        />

    <Button android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start">
        <requestFocus />
    </Button>

    <Button android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="chronometer_stop">
    </Button>

    <Button android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="reset">
        </Button>

    <Button android:id="@+id/set_format"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="format">
    </Button>

    <Button android:id="@+id/clear_format"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="clear_format">
    </Button>

    </LinearLayout>

I appreciate any help anyone can offer!


Solution

  • Your problem is that you are importing the wrong R class. android.R is different from the R file generated for your app and referencing your resources, it is the R file from the framework itself, allowing you to use convenience resources, see this question for an example.

    import android.R;
    

    replace that with

    import your.package.R;
    

    Also, as Thanos suggested, put your listener declarations inside the onCreate method.

    UPDATE :

    When I said your.package.R, it was just an example. The package depends on what you chose it to be when creating your project, but judging by the code you provided, you probably need import com.jackson.eason.stopwatch.R

    Secondly, you need to declare your listeners first before you use them. If you don't, if you call setOnClickListener(mClearFormatListener) but mClearFormatListener is declared after, the compiler doesn't know about mClearFormatListener yet so it shows a compilation error. The below code should work.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        // Get references to views
        mChronometer = (Chronometer) findViewById(R.id.Chronometer);
        Button button1 = (Button) findViewById(R.id.start);
        Button button2 = (Button) findViewById(R.id.stop);
        Button button3 = (Button) findViewById(R.id.reset);
        Button button4 = (Button) findViewById(R.id.set_format);
        Button button5 = (Button) findViewById(R.id.clear_format);
    
        // Declare the listeners
        View.OnClickListener mStartListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.start();
            }
        };
    
        View.OnClickListener mStopListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.stop();
            }
        };
    
        View.OnClickListener mResetListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setBase(SystemClock.elapsedRealtime());
            }
        };
    
       // You forgot to declare a listener for set format in your updated code
       View.OnClickListener mSetFormatListener = new OnClickListener() {
            public void onClick(View v) {
                // TODO : set the format
            }
        };
    
        View.OnClickListener mClearFormatListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setFormat(null);
            }
        };
    
        // Assign the listeners to your buttons
        button1.setOnClickListener(mStartListener);
        button2.setOnClickListener(mStopListener);
        button3.setOnClickListener(mResetListener);
        button4.setOnClickListener(mSetFormatListener);
        button.setOnClickListener(mClearFormatListener);
    
    
    }
    

    One last thing : in the notation mVariable, m stands for "member", meaning a member variable of a class, as opposed to a variable inside a method. Since your listeners are only declared inside a method, we usually don't use mListener but rather listener. Of course, this is just a naming convention and it will not prevent the code to compile and run ;)