Java 我不能在某些片段中使用ArrayAdapter制作旋转器
我试过用一些碎片做旋转器。我是通过Youtube上的教程做到这一点的。所以,我只是重写了教程中的所有内容 首先,在字符串中:ArrayAdapter adapter=newarrayadapter(这是android.R.layout.simple\u spinner\u项) 我的安卓工作室没有看到安卓。它只是用红色下划线,所以我写: ArrayAdapter=新的ArrayAdapter(this.getActivity(),android.R.layout.simple\u微调器\u项) 启动时没有错误,但当我尝试单击微调器时,应用程序崩溃 这是我的片段:Java 我不能在某些片段中使用ArrayAdapter制作旋转器,java,android,android-fragments,arraylist,Java,Android,Android Fragments,Arraylist,我试过用一些碎片做旋转器。我是通过Youtube上的教程做到这一点的。所以,我只是重写了教程中的所有内容 首先,在字符串中:ArrayAdapter adapter=newarrayadapter(这是android.R.layout.simple\u spinner\u项) 我的安卓工作室没有看到安卓。它只是用红色下划线,所以我写: ArrayAdapter=新的ArrayAdapter(this.getActivity(),android.R.layout.simple\u微调器\u项) 启
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();
}
}
和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如果您收到一个错误,说不能绑定,那么您需要先关闭adb服务器,然后重新启动它 adb杀死服务器 adb启动服务器
您可以从终端执行此操作我检查过它的可能副本
<?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>