我不能在某些片段中用阵列式帕特(Arrayadapter)进行旋转器



我尝试了一些片段中的旋转器。我在YouTube上使用教程。因此,我只是从该教程中重写。

首先,在字符串中:arrayadapter adapter = new arrayadapter(this,android.r.layout.simple_spinner_item(;

我的Android Studio没有看到Android。它只是红色下划线的,所以我写道:arrayadapter apapter = 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 debugger

如果您遇到错误说不能绑定,则需要先杀死它并再次启动ADB服务器。

ADB Kill-Server

adb start-server

您可以从终端

执行此操作

最新更新