简体   繁体   中英

First timer android development

I have been trying to work out my first android app with samples and code from the internet. Following them I got the following error

**"Brew_Time_Add cannot be resolved or is not a field."** 

for the line in the code

**Button brewAddTime = (Button) findViewById(R.id.brew_time_up);**

The xml for this part -

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:padding="10dip">
    <Button
      android:id="@+id/brew_time_down"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="-"
      android:textSize="40dip" />
    <TextView
      android:id="@+id/brew_time"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="0:00"
      android:textSize="40dip"
      android:padding="10dip" />
    <Button
      android:id="@+id/brew_time_up"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="+"
      android:textSize="40dip" />
  </LinearLayout>

The Java Code for the same -

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button brewAddTime = (Button) findViewById(R.id.brew_time_up);
        Button brewDecreaseTime = (Button) findViewById(R.id.brew_time_down);
            Button startBrew = (Button) findViewById(R.id.brew_start);
            TextView brewCountLabel = (TextView) findViewById(R.id.brew_count_label);
            TextView brewTimeLabel = (TextView) findViewById(R.id.brew_time);

    }

Why is the error and how can it be solved?

Please see image at- http://db.tt/kbjq6u5o

Thanks in advance.

Do you have such an image in your layout? If you have then save your project and clean it.

Got to project -> clean to clean it.

You are missing the xmls declaration in your layout - add xmlns:android="http://schemas.android.com/apk/res/android" to your layout, like so:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dip">
<Button
  android:id="@+id/brew_time_down"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="-"
  android:textSize="40dip" />
<TextView
  android:id="@+id/brew_time"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="0:00"
  android:textSize="40dip"
  android:padding="10dip" />
<Button
  android:id="@+id/brew_time_up"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="+"
  android:textSize="40dip" />

Though you will still have errors for both brew_start and brew_count_label as they are not in the xml.

Add these in the layout file. Your layout file is missing these 2 elements

<TextView
 android:id="@+id/brew_count_label"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="0:00"
 android:textSize="40dip"
 android:padding="10dip" />
 <Button
 android:id="@+id/brew_start"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Start"
 android:textSize="40dip" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM