开发者

Time Picker widget - "id cannot be resolved or is not a field"

开发者 https://www.devze.com 2023-02-11 17:02 出处:网络
I\'m getting the error for the following lines: mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);

I'm getting the error for the following lines:

mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime = (Button) findViewById(R.id.pickTime);

Although at this stage I'm merely copy-pasting stuff from the Tutorial, just to get a feel for it.. So where am I going wrong guys?

This is the .java file as a whole:

package com.example.hellotimepicker;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HelloTimePicker extends Activity {
    /** Called when the activity is first created. */

    private TextView mTimeDisplay;
    private Button mPickTime;

    private int开发者_开发问答 mHour;
    private int mMinute;

    static final int TIME_DIALOG_ID = 0;

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

        // capture our View elements
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mPickTime = (Button) findViewById(R.id.pickTime);

        // add a click listener to the button
        mPickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        // get the current time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // display the current date
        updateDisplay();

    }

 // updates the time we display in the TextView
    private void updateDisplay() {
        mTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
}


sounds like you missed to copy the layout (xml code) from the tutorial


Just want to share my experience with the original tutorial version because this might be useful. Earlier I used:

import android.R;

But the main cannot be resolved at the line:

setContentView(R.layout.main);

So I changed the "import android.R;" to:

import com.example.hellodatepicker.R; //to match with the package name

and found out it is running fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消