为什么Android在没有显示&#34; <application>意外停止的情况下关闭应用程序&#34;消息?

时间:2017-01-20 10:32:57

标签: android debugging

我正在学习Android开发,并试图在运行时崩溃我的应用程序。我创建了一个按钮,在单击时调用方法,然后确保该方法不存在。

应用程序启动正常。单击按钮后,应用程序将按预期关闭。唯一出乎意料的是,我的设备没有显示任何错误消息,例如应用程序意外关闭或应用程序强制关闭。

我是否意外关闭了“开发者选项”中的某些设置,还是仅仅是我的设备不显示错误消息?还需要更多信息吗?

activity_main.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:id="@+id/activity_main"
    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"
    android:orientation="vertical"
    tools:context="com.example.android.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quantity"
        android:textAllCaps="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/quantity_text_view"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:textColor="#000000"
        android:text="0"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:textAllCaps="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/price_text_view"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:textColor="#000000"
        android:text="$0"
        android:textSize="16sp" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:text="ORDER"
        android:onClick="submitOrder"/>
</LinearLayout>

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 submitOrders(View view) {
        int quantity = 5 ;
        display(quantity) ;
        displayPrice(quantity * 50) ;
    }

    /**
     * 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);
    }

    private void displayPrice(int number) {
       TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }
}

logcat的:

  

01-20 16:08:54.087 28870-28893 /? I / OpenGLRenderer:初始化的EGL,   版本1.4 01-20 16:08:54.087 28870-28893 /? D / OpenGLRenderer:交换   行为1 01-20 16:08:58.044 28870-28870 / com.example.android.justjava   V / BoostFramework:BoostFramework():mPerf =   com.qualcomm.qti.Performance@b82000f 01-20 16:08:58.119   28870-28870 / com.example.android.justjava D / AndroidRuntime:正在关闭   VM 01-20 16:08:58.120 28870-28870 / com.example.android.justjava   E / AndroidRuntime:致命异常:主要                                                                                 过程:com.example.android.justjava,PID:28870                                                                                 java.lang.IllegalStateException:找不到方法   用于android:onClick的父或祖先上下文中的submitOrder(View)   在视图类上定义的属性   android.support.v7.widget.AppCompatButton                                                                                     在   android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)                                                                                     在   android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)                                                                                     在android.view.View.performClick(View.java:5642)                                                                                     在android.view.View $ PerformClick.run(View.java:22338)                                                                                     在android.os.Handler.handleCallback(Handler.java:751)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                     在android.os.Looper.loop(Looper.java:154)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:6209)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)01-20   16:08:58.120 28870-28870 / com.example.android.justjava D / AppTracker:   应用事件:崩溃01-20 16:08:58.132 28870-28870 /?我/处理:发送   信号。 PID:28870 SIG:9

它基于this Udacity课程。

2 个答案:

答案 0 :(得分:0)

在您的XML按钮中,submitOrder不是submitOrders。查看额外的S

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:text="ORDER"
    android:onClick="submitOrder"/>

所以,改变一下:

public void submitOrders(View view) {

有了这个:

public void submitOrder(View view) {

答案 1 :(得分:0)

从方法名称submitOrders中删除s

public void submitOrder(View view) {
        int quantity = 5 ;
        display(quantity) ;
        displayPrice(quantity * 50) ;
    }

或者您可以删除ndroid:onClick="submitOrder"

<Button
    android:id="@+id/order"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:text="ORDER"/>

比你的java文件

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

        Button order = (Button)findViewById(R.id.order);
        order.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               int quantity = 5 ;
               display(quantity) ;
               displayPrice(quantity * 50) ;
           }
        });
  }