动态旋转器

时间:2014-05-13 21:46:12

标签: java android

我想在第一个微调器(县)上根据所选项填充第二个微调器(城市)。 Eclipse向我显示没有错误,但是当我运行应用程序并运行具有微调器的活动时,应用程序终止。如有任何更正/提示,我将不胜感激。

private Spinner County, cities;
ArrayAdapter<?> adapterTown;

County =(Spinner)findViewById(R.id.spinnercounty);
cities = (Spinner)findViewById(R.id.spinnerCities);

County.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {    

County.setSelection(pos);
int rId = getResources().getIdentifier(county,"array", getPackageName());                            
int pos1, pos2;

pos1 = County.getSelectedItemPosition();
int iden = parent.getId();

    if (iden == R.id.spinnercounty)
        {
         pos2 = 0;                  
         switch (pos) 
            {
             case 0: adapterTown = ArrayAdapter.createFromResource(
                    Registration.this, R.array.Atlantic, android.R.layout.simple_spinner_item);
                     break;

             case 1: adapterTown = ArrayAdapter.createFromResource(
                    Registration.this, R.array.Bergen, android.R.layout.simple_spinner_item);
                    break;

            case 2: adapterTown = ArrayAdapter.createFromResource(
                    Registration.this, R.array.Burligton, android.R.layout.simple_spinner_item);
                    break;

            case 3: adapterTown= ArrayAdapter.createFromResource(
                    Registration.this, R.array.Camden, android.R.layout.simple_spinner_item);
                    break;

            default:    
                    break;
            }

            cities.setAdapter(adapterTown);
            adapterTown.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// The problem might be here
        }

这是错误消息:

05-13 18:15:16.870: E/AndroidRuntime(845): FATAL EXCEPTION: main
05-13 18:15:16.870: E/AndroidRuntime(845): Process: com.example.msucarpool, PID: 845
05-13 18:15:16.870: E/AndroidRuntime(845): java.lang.NullPointerException: name is null
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.content.res.Resources.getIdentifier(Resources.java:1737)
05-13 18:15:16.870: E/AndroidRuntime(845):  at com.example.msucarpool.Registration$1.onItemSelected(Registration.java:74)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.widget.AdapterView.access$200(AdapterView.java:48)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.os.Handler.handleCallback(Handler.java:733)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.os.Looper.loop(Looper.java:136)
05-13 18:15:16.870: E/AndroidRuntime(845):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 18:15:16.870: E/AndroidRuntime(845):  at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:15:16.870: E/AndroidRuntime(845):  at java.lang.reflect.Method.invoke(Method.java:515)
05-13 18:15:16.870: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 18:15:16.870: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 18:15:16.870: E/AndroidRuntime(845):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案