安卓无法解析价格\文本\视图/Java
如何删除此错误
多亏了回购协议(见下面的链接),我用AppCompatActivithttps://gist.github.com/anonymous/6dde8cc012ac94aaf9ea取代了ActionBarActivity。但错误依然存在?为什么?我需要在代码中添加什么来删除错误
// MainActivity.java
package com.example.安卓.justjava;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.View;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.安卓.justjava.MainActivity"
安卓:orientation="vertical">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_margin="16dp"
安卓:text="Aantal"
安卓:textAllCaps="true"/>
<TextView
安卓:id="@+id/quantity_text_view"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_margin="16dp"
安卓:text="0"
安卓:textSize="16sp"
安卓:textColor="@安卓:color/black" />
<Button
安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="Bestellen"
安卓:onClick="submitOrder" />
# 1 楼答案
您使用:
TextView价格TextView=(TextView)findViewById(R.id.price\u text\u视图)
但是您的XML中没有任何具有此id的TextView。我想你在第一次文本视图中忘记了这个id