-1

I am new in Android Learning and try to complete my task, in which i need to create the Workout simple app, with dropdown, button and text. In app preview, app is looking fine but when i run on device it gives me an error "Unfortunately, FindWorkOutActivity has stopped working."

I have searched on internet about this error and unable to understand the Logcat information as i didn't learn about this. Kindly help me to solve this error.

Below are the Files of my app:

activity_find_work_out.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="com.example.rj.findworkout.FindWorkOutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/your_workout_here"
        android:id="@+id/text_hello"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button_id"
        android:layout_marginTop="20dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_id"
        android:text="New Button"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/spinner_id"
        android:layout_marginTop="20dp" />

    <Spinner
        android:id="@+id/spinner_id"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_marginTop="35dp"
        android:layout_centerHorizontal="true"
        android:entries="@array/work_out"
        android:onClick="onClickWorkOut"
        >

    </Spinner>
</RelativeLayout>

FindWorkOutActivity.java

package com.example.rj.findworkout;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Created by Rj on 5/25/2017.
 */

public class FindWorkOutActivity extends Activity {

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

    public void onClickWorkOut(View view){

        TextView workouts = (TextView) findViewById(R.id.text_hello);

        Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);

        String workout = String.valueOf(workouttype.getSelectedItem());

        workouts.setText(workout);


    }
}
Raza Javeid
  • 23
  • 10
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Note that `workouttype` has no data, and so it has no selected item. – CommonsWare May 25 '17 at 12:20
  • @CommonsWare thanks for advise, i am new in Android and no idea about LogCat as i didn't learn about it. – Raza Javeid May 25 '17 at 12:21
  • @CommonsWare please guide me how to solve this error. – Raza Javeid May 25 '17 at 13:00
  • I suggest that you read a book on Android app development. You cannot use the `Spinner` until you configure it with your `SpinnerAdapter`. See https://github.com/commonsguy/cw-omnibus/tree/v8.5/Selection/Spinner for a sample app that uses a `Spinner`. – CommonsWare May 25 '17 at 13:10

1 Answers1

1

Your problem is the onClick property in the Spinner. The proper way is to add setOnItemSelectedListener on it instead. So first you need to remove the

android:onClick="onClickWorkOut"

and in the Activity code, add a OnItemClickListener instead.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
           workouttype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

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

        }
    });
}
tompee
  • 1,408
  • 1
  • 7
  • 6
  • thanks for answer but i have no idea about adapterview, can you please help me to solve in simple. – Raza Javeid May 25 '17 at 12:56
  • Spinner is a subclass of AdapterView. AdapterView is a view in which its child views are determined by an Adapter. In the case of spinner, only 1 child view is visible at a time. To be able to handle the changes in the spinner, you need to set an OnItemSelelectedListener. This will have 2 callback functions onItemSelected and onNothingSelected. Their purpose are pretty self explanatory. The adapterView parameter tells you what spinner triggered the event. Since you only have 1 spinner, no need to worry about it. What you might be interested in is the position. – tompee May 25 '17 at 13:18
  • The position tells you what item in the list was selected. The position starts at 0 and correspond to the first item in the list. – tompee May 25 '17 at 13:22
  • i am not getting this issue, please help to understand as i am new learner in android. I have tried the same code as i learnt from video their code is running fine but i am getting this error. – Raza Javeid May 25 '17 at 17:42