在Android中使用微调器时,应用程序无法正常运行

时间:2018-01-12 22:23:59

标签: java android android-layout

在布局

中使用Button时终止应用程序

我在我的应用程序中使用了微调器小部件。切换声明......

这是我的 mainactivity.java 文件

package com.example.edward.converter;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import static java.lang.System.exit;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private TextView txt;
    private EditText etxt;
    private Spinner spn;

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

        btn = (Button) findViewById(R.id.button);
        txt = (TextView) findViewById(R.id.textView);
        etxt = (EditText) findViewById(R.id.editText);
        spn = (Spinner) findViewById(R.id.spinner);

        spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0 :
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                double result = 0.0;
                                double multiplier = 2.54;


                                if(etxt.getText().toString().equals("")){
                                    txt.setText(R.string.error);
                                    txt.setTextColor(Color.RED);
                                }else {
                                    double valueMetre = Double.parseDouble(etxt.getText().toString());
                                    result = valueMetre*multiplier;
                                    txt.setText(String.format("%.2f",result + " cm" ));
                                    txt.setVisibility(v.VISIBLE);
                                    txt.setTextColor(Color.WHITE);
                                }
                            }
                        });
                        break;
                    case 1 :
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                double result = 0.0;
                                double multiplier = 0.393701;


                                if(etxt.getText().toString().equals("")){
                                    txt.setText(R.string.error);
                                    txt.setTextColor(Color.RED);
                                }else {
                                    double valueMetre = Double.parseDouble(etxt.getText().toString());
                                    result = valueMetre*multiplier;
                                    txt.setText(String.format("%.2f",result + " Inch"));
                                    txt.setVisibility(v.VISIBLE);
                                    txt.setTextColor(Color.WHITE);
                                }
                            }
                        });
                        break;
                    case 2 :
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                double result = 0.0;
                                double multiplier = 0.0254;


                                if(etxt.getText().toString().equals("")){
                                    txt.setText(R.string.error);
                                    txt.setTextColor(Color.RED);
                                }else {
                                    double valueMetre = Double.parseDouble(etxt.getText().toString());
                                    result = valueMetre*multiplier;
                                    txt.setText(String.format("%.2f",result + " Metre"));
                                    txt.setVisibility(v.VISIBLE);
                                    txt.setTextColor(Color.WHITE);
                                }
                            }
                        });
                        break;
                    case 3 :
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                double result = 0.0;
                                double multiplier = 39.9701;


                                if(etxt.getText().toString().equals("")){
                                    txt.setText(R.string.error);
                                    txt.setTextColor(Color.RED);
                                }else {
                                    double valueMetre = Double.parseDouble(etxt.getText().toString());
                                    result = valueMetre*multiplier;
                                    txt.setText(String.format("%.2f",result + "Inch"));
                                    txt.setVisibility(v.VISIBLE);
                                    txt.setTextColor(Color.WHITE);
                                }
                            }
                        });
                        break;
                    case 4 :
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                double result = 0.0;
                                double multiplier = 0.01;


                                if(etxt.getText().toString().equals("")){
                                    txt.setText(R.string.error);
                                    txt.setTextColor(Color.RED);
                                }else {
                                    double valueMetre = Double.parseDouble(etxt.getText().toString());
                                    result = valueMetre*multiplier;
                                    txt.setText(String.format("%.2f",result + " Metre"));
                                    txt.setVisibility(v.VISIBLE);
                                    txt.setTextColor(Color.WHITE);
                                }
                            }
                        });
                        break;
                    case 5 :
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                double result = 0.0;
                                double multiplier = 100;


                                if(etxt.getText().toString().equals("")){
                                    txt.setText(R.string.error);
                                    txt.setTextColor(Color.RED);
                                }else {
                                    double valueMetre = Double.parseDouble(etxt.getText().toString());
                                    result = valueMetre*multiplier;
                                    txt.setText(String.format("%.2f",result + " cm"));
                                    txt.setVisibility(v.VISIBLE);
                                    txt.setTextColor(Color.WHITE);
                                }
                            }
                        });
                        break;
                    default:
                        break;
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                exit(0);

            }
        });



    }
}

&安培;这是 activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:background="@drawable/blue"
    tools:context="com.example.edward.converter.MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="205dp"
        android:layout_height="46dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:entries="@array/convert"
        android:gravity="center"
        android:longClickable="false"
        android:spinnerMode="dialog"
        app:layout_constraintBottom_toTopOf="@+id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="72dp"
        android:layout_height="71dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/todo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/converter" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="96dp"
        android:background="@drawable/circular"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/enter_value"
        android:inputType="textPersonName|number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <Button
        android:id="@+id/button"
        android:layout_width="112dp"
        android:layout_height="39dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circular"
        android:longClickable="true"
        android:text="@string/convert"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        app:layout_constraintVertical_bias="0.189" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="398dp"
        android:layout_height="45dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>

这是我在 strings.xml

中使用的内容
<resources>
    <string name="app_name">Converter</string>
    <string name="enter_value">Enter Value</string>
    <string name="todo">TODO</string>
    <string name="convert">Convert</string>
    <string name="error">Please Enter A Valid Number</string>
    <string-array name="convert">
        <item>Inch-cm</item>
        <item>cm-Inch</item>
        <item>Inch-Metre</item>
        <item>Metre-Inch</item>
        <item>cm-Metre</item>
        <item>Metre-cm</item>
    </string-array>
</resources

布局:

enter image description here

请帮助理解微调器概念&amp;这个错误...

0 个答案:

没有答案