我无法在某些片段中使用ArrayAdapter进行微调

时间:2019-05-11 14:43:12

标签: java android android-fragments arraylist

我曾尝试在某些片段中进行Spinner。我是在YouTube上的教程中这样做的。因此,我只是重写了该教程中的所有内容。

首先,在字符串中:ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item);

我的Android Studio没有看到android。它只是用红色下划线,所以我写: ArrayAdapter adapter = new ArrayAdapter(this.getActivity(),android.R.layout.simple_spinner_item);

启动时没有错误,但是当我尝试单击微调器时,应用程序崩溃了

这是我的片段:

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

    }


}

也是Java类

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

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

在日志中,我只有:16:28无法为调试器绑定到本地8600

1 个答案:

答案 0 :(得分:-1)

如果遇到错误消息说无法绑定,则需要先杀死它,然后重新启动,以重新启动adb服务器。

adb kill-server

adb启动服务器

您可以从终端上完成