我想在Fragment中使用Spinner,但它在监听器上给我一个错误。我知道这里有一些类似的问题,但我无法弄清楚我的问题是什么。问题是什么?这是我的onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.maps, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.onResume();
// Gets to GoogleMap from the MapView and does initialization stuff
mapView.getMapAsync(this)
// get Spinner reference
Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this); **<======ERROR======**
// Spinner Drop down elements
List<String> buses = new ArrayList<>();
buses.add("Összes megálló - válassz egy járatot");
buses.add("26");
buses.add("35");
buses.add("37");
buses.add("42");
// Creating array adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, buses);
// Drop down style will be listview with radio button
dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
return v;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
caseno = parent.getSelectedItemPosition();
switch(caseno) {
case 0: {
//some code
break;
}
case 1: {
//some code
break;
}
}
}
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getActivity(), "Nincs kiválasztott járat", Toast.LENGTH_LONG).show();
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/line"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@drawable/shadow">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="8dp"/>
</RelativeLayout>
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.koostamas.justtry, PID: 32434
java.lang.NullPointerException
at com.koostamas.justtry.Map.onCreateView(Map.java:151)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
AdapterView.OnItemClickListener
或者您可以试试这个
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
if (item != null) {
Toast.makeText(getContext(), item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(getContext(), "Selected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:1)
您需要实现您的微调器OnItemSelectedListener:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
//Called when item is selected, use position of item to find it from list of items
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
//Called when no item is selected
}
});
答案 2 :(得分:0)
在片段中使用微调器时,
奖金:自定义微调器样式
private Spinner SortBySpinner;
private SpinnerAdapter SortBy_adapterz;
public static final String[] SortAdapterz = {"",
" newest "," top brains "," most seen ",""
};
XML
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/TimeSpinner"/>
自定义您的适配器
oSpinner = (Spinner) v.findViewById(R.id.TimeSpinner);
oAdapter = new ArrayAdapter<String>(getActivity(),R.layout.cutomized_spinner, SortAdapterz);
cutomized_spinner.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_weight="1"
android:gravity="center"
android:text=" egypt "
android:fontFamily="sans-serif-smallcaps"
android:textColor="#464646"
android:textColorHint="#ffcc00"
android:padding="5dip"
android:layout_centerInParent="true"
/>
最后
oSpinner.setAdapter(oAdapter);
如何确定价值?
oSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cck = parent.getItemAtPosition(position).toString();
System.out.println("Value was --->" + cck );
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
结果