2

我正在尝试遵循手册(其中有一些错误并已修复)但是我收到此错误btstart cannot be resolved or is not a field并且我不知道如何修复它。这是该手册的链接(但我已经对布局和字符串 xml 文件进行了更改)这是 MainActivity.java 代码:

 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());

        }

    }

}

这是我的“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>

这是“strings.xml”文件:

    <?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>

我没有对 main.xml 进行任何更改,这是我的 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>

您能否让我知道我缺少什么以及此错误的解决方法是什么?

PS:我收到这些行的类似错误:

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);

万分感谢。

4

3 回答 3

3

就像 Sotirios 说的,尝试清理项目,然后在 Project => 中自动取消选中清理,然后现在您可以单独构建您的项目。

于 2013-09-10T02:09:12.607 回答
0

R 没有被解析是因为 没有自动生成R.java文件。

检查资源文件夹中的错误,因为这是R.java自动生成的基础。就我而言,在我的AndroidManifest.xml文件中,我必须将其中一行从

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

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

(即更改为资源(res)文件夹中的.png文件@drawable/icon的名称

于 2014-03-20T06:43:11.230 回答
0

看到这个“id cannot be resolved or is not a field”错误?它可能会帮助你。

看看你是否正在导入 android .R

那可能是错误

于 2013-09-10T00:55:05.830 回答