Home Application can't run properly while using spinner in Android
Reply: 0

Application can't run properly while using spinner in Android

user12775
1#
user12775 Published in April 26, 2018, 5:24 pm

application terminated when using the Button in Layout

I have used spinner widget for my application & switch statements...

this is my mainactivity.java file

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);

            }
        });



    }
}

& this is activity_main.xml file

<?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>

This is what i have used in 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

layout:

Please Help in understanding this spinner concept & this error...

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.43153 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO