无法解决price_text_view / Java

时间:2017-03-25 18:01:04

标签: java android

如何删除此错误?

感谢repo(请参阅下面的链接),我已经通过AppCompatActivit https://gist.github.com/anonymous/6dde8cc012ac94aaf9ea替换了ActionBarActivity。但错误仍然存​​在?怎么会?我需要更改或添加代码以删除错误?

// MainActivity.java
package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

int quantity = 2;

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

/**
 * This method is called when the plus button is clicked.
 */
public void increment(View view) {
    quantity = quantity + 1;
    display(quantity);
}

/**
 * This method is called when the minus button is clicked.
 */
public void decrement(View view) {
    quantity = quantity - 1;
    display(quantity);
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    int quantity = 5;
    display(quantity);
    displayPrice(quantity * 5);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

/**
 * This method displays the given price value on the screen.
 */
private void displayPrice(int number) {
    TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
    priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}

}

// activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.justjava.MainActivity"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="Aantal"
android:textAllCaps="true"/>

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="0"
    android:textSize="16sp"
    android:textColor="@android:color/black" />

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginTop="16dp"
    android:text="Bestellen"
    android:onClick="submitOrder" />

1 个答案:

答案 0 :(得分:0)

您使用:

TextView priceTextView =(TextView)findViewById(R.id.price_text_view);

但是你的XML中没有任何带有此id的TextView。我想你已经忘记了第一个TextView中的这个id。