-1

I had tried do Spinner in some Fragment. I was doing that with tutorial on Youtube. So, I just rewrite all from that tutorial.

Firstly, in string : ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);

my Android Studio didn`t saw android. It is just underlined in red, so I write : ArrayAdapter adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_spinner_item);

there were no errors at startup, but when I try to click on the spinner, the application crashes

This is my Fragment:

package com.example.itss;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class SettingsFragment extends Fragment {

    private Spinner timeOfSleep;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.settings_fragment,container, false);

        timeOfSleep = v.findViewById(R.id.timeOfsleep);

        List<TimeOfSleep> timeOfSleepList = new ArrayList<>();
        TimeOfSleep time5 = new TimeOfSleep(5);
        timeOfSleepList.add(time5);
        TimeOfSleep time10 = new TimeOfSleep(10);
        timeOfSleepList.add(time10);
        TimeOfSleep time15 = new TimeOfSleep(15);
        timeOfSleepList.add(time15);

        ArrayAdapter<TimeOfSleep> adapter = new ArrayAdapter<TimeOfSleep>(this.getActivity(), android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        timeOfSleep.setAdapter(adapter);
        timeOfSleep.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TimeOfSleep tOS = (TimeOfSleep) parent.getSelectedItem();
                displaytimeOfSleep(tOS);
            }

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

            }
        });




        return v;
    }

    public void getSelectedtimeOfSleep(View v){
        TimeOfSleep tOS = (TimeOfSleep) timeOfSleep.getSelectedItem();
        displaytimeOfSleep(tOS);
    }

    private void displaytimeOfSleep(TimeOfSleep a){
        int time = a.getTimeOfSleep();

    }


}

Also java class

package com.example.itss;

public class TimeOfSleep {

    private int timeOfSleep;


    public TimeOfSleep(int timeOfSleep) {
        this.timeOfSleep = timeOfSleep;
    }

    public int getTimeOfSleep() {
        return timeOfSleep;
    }

    public void setTimeOfSleep(int timeOfSleep) {
        this.timeOfSleep = timeOfSleep;
    }

    @Override
    public String toString() {
        String a = timeOfSleep + " мин";
        return a;
    }
}

And xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000"
    android:padding="25dp"
    android:paddingBottom="100dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="50dp">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Время засыпания:"
                android:background="@color/colorPrimary"
                android:textColor="#FFFFFF"
                android:onClick="getSelectedtimeOfSleep"/>

             <Spinner
                 android:layout_width="50dp"
                 android:layout_height="50dp"
                 android:background="@drawable/ic_arrow_drop_down_black_24dp"
                 android:id="@+id/timeOfsleep">

             </Spinner>
        </LinearLayout>

</LinearLayout>

In Logs I have just: 16:28 Can't bind to local 8600 for debugger

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

-1

If you are getting an error saying can't bind, then you need to restart your adb server by killing it first and starting it again.

adb kill-server

adb start-server

You can do this from the terminal