当前位置: 首页 > 知识库问答 >
问题:

Android Studio运行时错误(按钮)

汪飞捷
2023-03-14

如果你能帮我解决这个问题,我会非常非常感激的。

我已经审阅了logcat,但不能理解它,除了按钮(onClick)是问题所在。我不知道怎么修好它。

这在logcat中每隔几秒钟就会以红色显示:E/TZ_CCM_Server:仅支持“CCM”

以下是与运行时崩溃相关的完整错误日志:

编辑: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 {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int coffeeNumber = 3;
        display(coffeeNumber);
        displayPrice(coffeeNumber * 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 on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }


}

XML代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
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="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
    android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quantity"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:textAllCaps="true"
    android:layout_marginBottom="16dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textSize="16sp"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:id="@+id/quantity_text_view"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/black"
        android:padding="10dp"
        android:textAllCaps="true"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="€0"
        android:textSize="16sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/black"
        android:padding="10dp"
        android:layout="@+id/price_text_view"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Order"
        android:onClick="submitOrder"
        />


</LinearLayout>

共有1个答案

金皓君
2023-03-14

检查您的activity_main.xml在此布局中您没有id为price_text_view的TextView。只需使用该id定义一个TextView,您的应用程序就不会再崩溃,您可以继续下一章;)

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

我想你只是犯了一个愚蠢的错误。我看到你用的是布局而不是id^^

 类似资料:
  • 问题内容: 我是mysql和jdbc的新手,但出现此标题错误。我整天都在搜索,找不到适合我的解决方案。 我尝试过的操作:卸载/重新安装mysql,将mysql-connector- java-5.1.25-bin.jar和ojdbc7.jar复制粘贴到与我要运行的.class文件相同的位置,然后将该程序重建在其他目录中,可能还有其他几件事。 我正在使用notepad ++进行编码,并使用Windo

  • 我正在尝试仅使用本地依赖项编译和运行java grpc客户端,但出现以下错误: 这是我的gradle文件: 程序将编译,但不运行。我已经从protos生成了我的java文件,并验证了我是否使用了正确的protoc和protoc gen grpc java与我正在使用的jar库相对应。非常感谢您的帮助。

  • 我收到一个奇怪的错误。在我初始化总数的线上。如果你有空闲时间帮我,我不明白; 第10行:Char 24:运行时错误:-inf超出了“int”(solution.cpp)类型的可表示值的范围摘要:UndefinedBehaviorSanitizer:undefined behavior prog_joined。cpp:19:24

  • 这个程序应该在一个JFrame中添加两个面板,每个面板都有一个开关按钮,允许用户在每个面板之间切换。它编译得很好,但是当我试图单击Switch1按钮时,我得到了这个错误: java线程“AWT-EventQueue-0”中出现异常。ClassCastException:java。awt。无法将BorderLayout转换为java。awt。测试仪上的卡片布局。在javax上执行的操作(tester

  • 我试图插入到一个MySQL数据库,代码工作和插入,但立即崩溃,看不出问题。 Applications.loginandRegister e/AndroidRuntime:致命异常:主进程:Applications.loginandRegister,PID:2545 Android.view.WindowManager$BadTokenException:无法添加窗口--令牌Android.os.B