如何制作“拆分它!”按钮在我的应用中工作?

时间:2017-10-06 16:57:39

标签: android

这是我的第一个应用程序项目之一,我非常兴奋!我无法使“拆分”按钮工作(它必须将总数除以“人”中设置的人数)。第一个百分比按钮工作正常,但不是最后一个“拆分它!”。

我正在学习很多,我喜欢它!

xml.file

    <?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/activiy_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context="com.example.dell.calculator.MainActivity"
    android:weightSum="1">


    <EditText
        android:id="@+id/subtotal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Subtotal"
        android:inputType="number|numberDecimal" />

    <EditText
        android:id="@+id/itbms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="ITBMS"
        android:inputType="number|numberDecimal" />

    <Button
        android:id="@+id/diez"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="10%" />

    <Button
        android:id="@+id/quince"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="15%" />

    <Button
        android:id="@+id/veinte"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="20%" />

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.18"
        android:text="TOTAL"
        android:textSize="25dp"
        android:inputType="numberDecimal"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Number of People"
        android:layout_weight="0.09" />

    <EditText
        android:id="@+id/people"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="25dp"
        android:inputType="number" />

    <Button
        android:id="@+id/split"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Split It!" />

    <EditText
        android:id="@+id/finalresult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.55"
        android:ems="10"
        android:hint="EACH"
        android:inputType="numberDecimal" />

</LinearLayout>

Mainactivity.java

    package com.example.dell.calculator;

import android.icu.text.NumberFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Locale;

import static com.example.dell.calculator.R.id.add;

public class MainActivity extends AppCompatActivity {

    TextView result, finalresult;
    EditText subtotal, itbms, people;
    Button diez, quince, veinte, split;

    float result_num, result_num2;
    int num1, num2, num3, num4;


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

        result = (TextView) findViewById(R.id.result);
        finalresult = (TextView) findViewById(R.id.finalresult);

        itbms = (EditText) findViewById(R.id.itbms);
        subtotal = (EditText) findViewById(R.id.subtotal);

        diez = (Button) findViewById(R.id.diez);
        quince = (Button) findViewById(R.id.quince);
        veinte = (Button) findViewById(R.id.veinte);

        diez.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                num1 = Integer.parseInt(subtotal.getText().toString());
                num2 = Integer.parseInt(itbms.getText().toString());
                result_num = (float) ((num1 + num2) * 1.10);
                result.setText(String.valueOf(result_num));
            }
        });

        quince.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                num1 = Integer.parseInt(subtotal.getText().toString());
                num2 = Integer.parseInt(itbms.getText().toString());
                result_num = (float) ((num1 + num2) * 1.15);
                result.setText(String.valueOf(result_num));
            }

        });

        veinte.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                num1 = Integer.parseInt(subtotal.getText().toString());
                num2 = Integer.parseInt(itbms.getText().toString());
                result_num = (float) ((num1 + num2) * 1.20);
                result.setText(String.valueOf(result_num));
            }

        });


        split.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                num4 = (int) result_num;
                num3 = Integer.parseInt(people.getText().toString());
                result_num2 = (float) num4 / num3;
                finalresult.setText(String.valueOf(result_num2));

            }

        });


    }


}

2 个答案:

答案 0 :(得分:0)

您忘记获取按钮视图参考:

改变这个:

package arrays.assignment.pkg1;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JOptionPane;

public class ArraysAssignment1 {

public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
"Jack", "Queen", "King"};

// Initialize cards
for (int i = 0; i < deck.length; i++) {
  deck[i] = i;
}

// Display the all the cards
for (int i = 0; i < 52; i++) {
    String suit = suits[deck[i] / 13];
    String rank = ranks[deck[i] % 13];
    System.out.println( rank + " of " + suit);
}
// Shuffle the cards
for (int i = 0; i < deck.length; i++) {
  int index = (int)(Math.random() * deck.length);
  int temp = deck[i];
  deck[i] = deck[index];
  deck[index] = temp;
}
    System.out.println("your deck consists of\n");


System.out.println("\n\nin your hand, you have\n");

// Display 5 cards
for (int i = 0; i <5;i++) {
    String suit = suits[deck[i] / 13];
    String rank = ranks[deck[i] %13];
    System.out.println( rank + " of " + suit);
}
    //Display the cards that are greater than 9
    System.out.println("\nThe cards in your hand that are greater than 9 are\n");
    for (int i = 0; i <5;i++) {
    if(deck[i] > 9) {

        System.out.println(rank + " of " + suit);
    }
}
}

为此:

        diez = (Button) findViewById(R.id.diez);
        quince = (Button) findViewById(R.id.quince);
        veinte = (Button) findViewById(R.id.veinte);

通过阅读应用日志可以检测到这种错误。我确信由 diez = (Button) findViewById(R.id.diez); quince = (Button) findViewById(R.id.quince); veinte = (Button) findViewById(R.id.veinte); split = (Button) findViewById(R.id.split); 的调用引起的NullPointerException ocurrs(分裂为split.setOnClickListener)。

答案 1 :(得分:0)

这对初学者来说是完全正常的。我建议你声明并初始化View Objects(Button,TextView..etc) 同时进行。

Button split = (Button) findViewById(R.id.split);