【问题标题】:No error in code. But while launching the app says, Unfortunately app has stopped [duplicate]代码没有错误。但是在启动应用程序时说,不幸的是应用程序已停止[重复]
【发布时间】:2020-05-26 07:11:11
【问题描述】:

我开发了简单的安卓应用程序,包括一些简单的加法计算功能。运行模拟器后显示那种错误。在 android 应用程序中发现该错误浪费了更多时间。代码没有错误。但是在启动应用程序时说,不幸的是应用程序已停止。

Error- Screen Shot

我的代码如下

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/textView_answer"
        android:layout_width="100dp"
        android:layout_height="25dp"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="300dp"
        android:text="0"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView_first_no"
        android:layout_width="150dp"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="50dp"
        android:text="First number"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView_second_no"
        android:layout_width="150dp"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="100dp"
        android:text="Second number"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/editText_first_no"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="40dp"
        android:inputType="number"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/editText_second_no"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="90dp"
        android:inputType="number"
        tools:ignore="MissingConstraints" />


</RelativeLayout>

MainActivity.java

package com.example.marks;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

    EditText number1;
    EditText number2;
    Button Add_button;
    TextView result;

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

        number1 = (EditText) findViewById(R.id.editText_first_no);
        number2 = (EditText) findViewById(R.id.editText_second_no);
        result = (TextView) findViewById(R.id.textView_answer);

        Add_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                double num1 = Double.parseDouble(number1.getText().toString());
                double num2 = Double.parseDouble(number2.getText().toString());

                double sum = num1 + num2;

                result.setText(Double.toString(sum));
            }
        });
    }
}

【问题讨论】:

  • logcat 说什么?
  • @federico-klez-culloca 没有任何东西..
  • 不可能。 logcat中会有异常。
  • @Sergey Glotov 没有说 logcat。当然。但是xml文件查看一些警告。
  • @Mike M 谢谢。我发现了

标签: java android android-studio


【解决方案1】:

xml 文件上的按钮 (Add_button) 的位置。 错误是因为按钮尚未定义。

【讨论】:

  • 添加按钮后。但错误是一样的
  • 你也应该通过 add_button =findviewbyid 来初始化它...
  • 按钮 Add_button = findViewById(R .id.submit);
猜你喜欢
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多