我不知道为什么这个应用程序崩溃了。我正在尝试为城市和地区执行动态微调器。我想根据从第一个微调器中选择的城市自动填充区域微调器。任何帮助将不胜感激。提前谢谢。
select_location_layout.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:background="#C51162"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:text="@string/selectCity"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/citySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:spinnerMode="dialog" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/selectArea"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/areaSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:spinnerMode="dialog" />
</LinearLayout>
LocationSelection.java
package com.pixalstudio.navigationdrawer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class LocationSelection extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_location_layout);
Spinner citySpinner = (Spinner) findViewById(R.id.citySpinner);
Spinner areaSpinner = (Spinner) findViewById(R.id.areaSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
citySpinner.setAdapter(adapter);
final ArrayAdapter<CharSequence> adapter1 = ArrayAdapter
.createFromResource(this, R.array.ahmedabad_area,
android.R.layout.simple_spinner_item);
areaSpinner.setAdapter(adapter1);
citySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
adapter1.clear();
int stringArrayResource = 0;
switch (position) {
case 0:
stringArrayResource = R.array.ahmedabad_area;
break;
case 1:
stringArrayResource = R.array.baroda_area;
break;
case 2:
stringArrayResource = R.array.surat_area;
break;
case 3:
stringArrayResource = R.array.mumbai_area;
break;
case 4:
stringArrayResource = R.array.pune_area;
break;
case 5:
stringArrayResource = R.array.banglore_area;
break;
}
adapter1.addAll(getResources().getStringArray(
stringArrayResource));
adapter1.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Navigation Drawer</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="drawer_open">Open Navigation Drawer</string>
<string name="drawer_close">Close Navigation Drawer</string>
<string name="selectCity">Select City</string>
<string name="selectArea">Select Area</string>
<string-array name="drawerItems">
<item>Location</item>
<item>Ready for Delivery</item>
<item>Order Cake</item>
<item>About us</item>
<item>Setting</item>
<item>Login</item>
<item>My Orders</item>
<item>My Cart</item>
<item>Help</item>
<item>Contact us</item>
<item>Rate us</item>
<item>Share</item>
</string-array>
<string-array name="cities">
<item>Ahmedabad</item>
<item>Baroda</item>
<item>Surat</item>
<item>Mumbai</item>
<item>Pune</item>
<item>Banglore</item>
</string-array>
<string-array name="ahmedabad_area">
<item>Maninagar</item>
<item>C.T.M</item>
<item>C.G. Road</item>
<item>Satellite</item>
<item>S.G. Highway</item>
<item>Navarangpura</item>
</string-array>
<string-array name="baroda_area">
<item>Alkapuri</item>
<item>Fateganj</item>
<item>Jaeahar Nagar</item>
<item>Karali</item>
<item>Mandavi</item>
<item>Masar Road</item>
</string-array>
<string-array name="surat_area">
<item>Athwa</item>
<item>Bajipura</item>
<item>Cenral Colony</item>
<item>Hajira</item>
<item>Kamrej</item>
<item>Lajpor</item>
</string-array>
<string-array name="mumbai_area">
<item>Adarsha Nagar</item>
<item>Thane</item>
<item>Centacruz</item>
<item>Andheri</item>
<item>Varsova</item>
<item>Dadar</item>
</string-array>
<string-array name="pune_area">
<item>Akurdi</item>
<item>Bavada</item>
<item>Chakan</item>
<item>Daund</item>
<item>Earth Stn</item>
<item>Ganeshkhind</item>
</string-array>
<string-array name="banglore_area">
<item>Indira Nagar</item>
<item>Jay Nagar</item>
<item>TipSandra</item>
<item>Kora Mangala</item>
<item>Rajaji Nagar</item>
<item>White Field</item>
</string-array>
</resources>
logcat的
07-08 09:43:59.546: D/OpenGLRenderer(3140): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-08 09:43:59.549: D/(3140): HostConnection::get() New Host Connection established 0xb42d7750, tid 3140
07-08 09:43:59.610: D/Atlas(3140): Validating map...
07-08 09:43:59.727: D/libEGL(3140): loaded /system/lib/egl/libEGL_emulation.so
07-08 09:43:59.727: D/libEGL(3140): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-08 09:43:59.745: D/libEGL(3140): loaded /system/lib/egl/libGLESv2_emulation.so
07-08 09:43:59.774: D/(3140): HostConnection::get() New Host Connection established 0xaf039480, tid 3158
07-08 09:43:59.888: I/OpenGLRenderer(3140): Initialized EGL, version 1.4
07-08 09:44:00.027: D/OpenGLRenderer(3140): Enabling debug mode 0
07-08 09:44:00.063: W/EGL_emulation(3140): eglSurfaceAttrib not implemented
07-08 09:44:00.063: W/OpenGLRenderer(3140): Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035800, error=EGL_SUCCESS
07-08 09:44:07.986: W/EGL_emulation(3140): eglSurfaceAttrib not implemented
07-08 09:44:07.986: W/OpenGLRenderer(3140): Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43d4c00, error=EGL_SUCCESS
07-08 09:44:08.032: D/AndroidRuntime(3140): Shutting down VM
07-08 09:44:08.033: E/AndroidRuntime(3140): FATAL EXCEPTION: main
07-08 09:44:08.033: E/AndroidRuntime(3140): Process: com.pixalstudio.navigationdrawer, PID: 3140
07-08 09:44:08.033: E/AndroidRuntime(3140): java.lang.UnsupportedOperationException
07-08 09:44:08.033: E/AndroidRuntime(3140): at java.util.AbstractList.remove(AbstractList.java:638)
07-08 09:44:08.033: E/AndroidRuntime(3140): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
07-08 09:44:08.033: E/AndroidRuntime(3140): at java.util.AbstractList.removeRange(AbstractList.java:658)
07-08 09:44:08.033: E/AndroidRuntime(3140): at java.util.AbstractList.clear(AbstractList.java:466)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258)
07-08 09:44:08.033: E/AndroidRuntime(3140): at com.pixalstudio.navigationdrawer.LocationSelection$1.onItemSelected(LocationSelection.java:34)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.widget.AdapterView.fireOnSelected(AdapterView.java:914)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:903)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.widget.AdapterView.access$300(AdapterView.java:48)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:873)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.os.Handler.handleCallback(Handler.java:739)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.os.Handler.dispatchMessage(Handler.java:95)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.os.Looper.loop(Looper.java:135)
07-08 09:44:08.033: E/AndroidRuntime(3140): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-08 09:44:08.033: E/AndroidRuntime(3140): at java.lang.reflect.Method.invoke(Native Method)
07-08 09:44:08.033: E/AndroidRuntime(3140): at java.lang.reflect.Method.invoke(Method.java:372)
07-08 09:44:08.033: E/AndroidRuntime(3140): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-08 09:44:08.033: E/AndroidRuntime(3140): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
NEW LOGCAT
07-08 10:05:38.686: D/OpenGLRenderer(3587): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-08 10:05:38.694: D/(3587): HostConnection::get() New Host Connection established 0xb42d7710, tid 3587
07-08 10:05:38.720: D/Atlas(3587): Validating map...
07-08 10:05:38.822: D/libEGL(3587): loaded /system/lib/egl/libEGL_emulation.so
07-08 10:05:38.823: D/libEGL(3587): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-08 10:05:38.855: D/libEGL(3587): loaded /system/lib/egl/libGLESv2_emulation.so
07-08 10:05:38.869: D/(3587): HostConnection::get() New Host Connection established 0xaf039480, tid 3605
07-08 10:05:38.967: I/OpenGLRenderer(3587): Initialized EGL, version 1.4
07-08 10:05:39.104: D/OpenGLRenderer(3587): Enabling debug mode 0
07-08 10:05:39.278: W/EGL_emulation(3587): eglSurfaceAttrib not implemented
07-08 10:05:39.278: W/OpenGLRenderer(3587): Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035800, error=EGL_SUCCESS
07-08 10:05:39.335: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=24909, eventTime=11265296, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:39.339: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=24910, eventTime=11265576, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:39.340: I/Choreographer(3587): Skipped 34 frames! The application may be doing too much work on its main thread.
07-08 10:05:39.615: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=24911, eventTime=11266094, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:39.615: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=24912, eventTime=11266144, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:39.616: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=24913, eventTime=11266373, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:50.356: W/EGL_emulation(3587): eglSurfaceAttrib not implemented
07-08 10:05:50.356: W/OpenGLRenderer(3587): Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43d4be0, error=EGL_SUCCESS
07-08 10:05:50.383: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=25119, eventTime=11276982, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:50.389: W/ViewRootImpl(3587): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=25120, eventTime=11277095, downTime=9978220, deviceId=1, source=0x301 }
07-08 10:05:50.429: D/AndroidRuntime(3587): Shutting down VM
07-08 10:05:50.430: E/AndroidRuntime(3587): FATAL EXCEPTION: main
07-08 10:05:50.430: E/AndroidRuntime(3587): Process: com.pixalstudio.navigationdrawer, PID: 3587
07-08 10:05:50.430: E/AndroidRuntime(3587): java.lang.UnsupportedOperationException
07-08 10:05:50.430: E/AndroidRuntime(3587): at java.util.AbstractList.add(AbstractList.java:404)
07-08 10:05:50.430: E/AndroidRuntime(3587): at java.util.AbstractList.add(AbstractList.java:425)
07-08 10:05:50.430: E/AndroidRuntime(3587): at java.util.Collections.addAll(Collections.java:2583)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:211)
07-08 10:05:50.430: E/AndroidRuntime(3587): at com.pixalstudio.navigationdrawer.LocationSelection$1.onItemSelected(LocationSelection.java:57)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.widget.AdapterView.fireOnSelected(AdapterView.java:914)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:903)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.widget.AdapterView.access$300(AdapterView.java:48)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:873)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.os.Handler.handleCallback(Handler.java:739)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.os.Handler.dispatchMessage(Handler.java:95)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.os.Looper.loop(Looper.java:135)
07-08 10:05:50.430: E/AndroidRuntime(3587): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-08 10:05:50.430: E/AndroidRuntime(3587): at java.lang.reflect.Method.invoke(Native Method)
07-08 10:05:50.430: E/AndroidRuntime(3587): at java.lang.reflect.Method.invoke(Method.java:372)
07-08 10:05:50.430: E/AndroidRuntime(3587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-08 10:05:50.430: E/AndroidRuntime(3587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
我说adapter1.clear();
是非法的并且抛出:
java.lang.UnsupportedOperationException
因为有些东西在列表上迭代。因为com.pixalstudio.navigationdrawer.LocationSelection$1.onItemSelected(LocationSelection.java:34)
删除第34行adapter1.clear();
new strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Navigation Drawer</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="drawer_open">Open Navigation Drawer</string>
<string name="drawer_close">Close Navigation Drawer</string>
<string name="selectCity">Select City</string>
<string name="selectArea">Select Area</string>
</resources>
添加array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="drawerItems">
<item>Location</item>
<item>Ready for Delivery</item>
<item>Order Cake</item>
<item>About us</item>
<item>Setting</item>
<item>Login</item>
<item>My Orders</item>
<item>My Cart</item>
<item>Help</item>
<item>Contact us</item>
<item>Rate us</item>
<item>Share</item>
</string-array>
<string-array name="cities">
<item>Ahmedabad</item>
<item>Baroda</item>
<item>Surat</item>
<item>Mumbai</item>
<item>Pune</item>
<item>Banglore</item>
</string-array>
<string-array name="ahmedabad_area">
<item>Maninagar</item>
<item>C.T.M</item>
<item>C.G. Road</item>
<item>Satellite</item>
<item>S.G. Highway</item>
<item>Navarangpura</item>
</string-array>
<string-array name="baroda_area">
<item>Alkapuri</item>
<item>Fateganj</item>
<item>Jaeahar Nagar</item>
<item>Karali</item>
<item>Mandavi</item>
<item>Masar Road</item>
</string-array>
<string-array name="surat_area">
<item>Athwa</item>
<item>Bajipura</item>
<item>Cenral Colony</item>
<item>Hajira</item>
<item>Kamrej</item>
<item>Lajpor</item>
</string-array>
<string-array name="mumbai_area">
<item>Adarsha Nagar</item>
<item>Thane</item>
<item>Centacruz</item>
<item>Andheri</item>
<item>Varsova</item>
<item>Dadar</item>
</string-array>
<string-array name="pune_area">
<item>Akurdi</item>
<item>Bavada</item>
<item>Chakan</item>
<item>Daund</item>
<item>Earth Stn</item>
<item>Ganeshkhind</item>
</string-array>
<string-array name="banglore_area">
<item>Indira Nagar</item>
<item>Jay Nagar</item>
<item>TipSandra</item>
<item>Kora Mangala</item>
<item>Rajaji Nagar</item>
<item>White Field</item>
</string-array>
</resources>
新LocationSelection.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class LocationSelection extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_location_layout);
Spinner citySpinner = (Spinner) findViewById(R.id.citySpinner);
Spinner areaSpinner = (Spinner) findViewById(R.id.areaSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
citySpinner.setAdapter(adapter);
//create new ArrayAdapter without loading from resource... Loading from resource makes it final
final ArrayAdapter<CharSequence> adapter1 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
areaSpinner.setAdapter(adapter1);
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapter1.clear();
int stringArrayResource = 0;
switch (position) {
case 0:
stringArrayResource = R.array.ahmedabad_area;
break;
case 1:
stringArrayResource = R.array.baroda_area;
break;
case 2:
stringArrayResource = R.array.surat_area;
break;
case 3:
stringArrayResource = R.array.mumbai_area;
break;
case 4:
stringArrayResource = R.array.pune_area;
break;
case 5:
stringArrayResource = R.array.banglore_area;
break;
}
adapter1.addAll(getResources().getStringArray(stringArrayResource));
adapter1.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}