0

如何修复此错误?我找不到任何可以帮助我的东西!代码片段将不胜感激!

sp2 的适配器在 sp1 的 onItemSelected 中设置的原因是因为我希望 sp2 的输出依赖于 sp1 中的选择。

日志猫:

    08-05 16:36:24.856: D/dalvikvm(6606): GC_EXTERNAL_ALLOC freed 81K, 43% free 3119K/5379K, external 2770K/2773K, paused 28ms
08-05 16:36:25.707: D/AndroidRuntime(6606): Shutting down VM
08-05 16:36:25.707: W/dalvikvm(6606): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
08-05 16:36:25.707: E/AndroidRuntime(6606): FATAL EXCEPTION: main
08-05 16:36:25.707: E/AndroidRuntime(6606): java.lang.NullPointerException
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:113)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at com.madonk.louisiana.festivals.Region$1.onItemSelected(Region.java:107)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:877)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.widget.AdapterView.access$200(AdapterView.java:42)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:843)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.os.Handler.handleCallback(Handler.java:587)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.os.Looper.loop(Looper.java:130)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at android.app.ActivityThread.main(ActivityThread.java:3821)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at java.lang.reflect.Method.invoke(Method.java:507)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-05 16:36:25.707: E/AndroidRuntime(6606):     at dalvik.system.NativeStart.main(Native Method)

这是有问题的代码:

sp2.setAdapter(sc_cit_adp);

.java:

package com.madonk.louisiana.festivals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * @author madonk
 *
 */
public class Region extends Activity {

Spinner sp1,sp2,sp3;
ArrayAdapter<String> reg_adp,sw_cit_adp,sc_cit_adp,sw_lake_charles_adp,sw_iowa_adp,sc_lafayette_adp,sc_gueydan_adp;
List<String> regions,sw_cities,sc_cities,sw_lake_charles,sw_iowa,sc_lafayette,sc_gueydan;

int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_region);

    String[] regionsArray = getResources().getStringArray(R.array.regions_array);
    regions = new ArrayList<String>(Arrays.asList(regionsArray));

    String[] swcitiesArray = getResources().getStringArray(R.array.southwest_cities_array);
    sw_cities = new ArrayList<String>(Arrays.asList(swcitiesArray));

    String[] swiowaArray = getResources().getStringArray(R.array.sw_iowa_array);
    sw_iowa = new ArrayList<String>(Arrays.asList(swiowaArray));

    String[] swlakecharlesArray = getResources().getStringArray(R.array.sw_lake_charles_array);
    sw_lake_charles = new ArrayList<String>(Arrays.asList(swlakecharlesArray));

    String[] sclafayetteArray = getResources().getStringArray(R.array.sc_lafayette_array);
    sc_lafayette = new ArrayList<String>(Arrays.asList(sclafayetteArray));

    String[] scgueydanArray = getResources().getStringArray(R.array.sc_gueydan_array);
    sc_gueydan = new ArrayList<String>(Arrays.asList(scgueydanArray));

    sp1= (Spinner) findViewById(R.id.regions_spinner);
    sp2= (Spinner) findViewById(R.id.city_spinner);
    sp3= (Spinner) findViewById(R.id.festival_spinner);

    reg_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,regions);
    reg_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sp1.setAdapter(reg_adp);

    sw_cit_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sw_cities);
    sw_cit_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    sw_lake_charles_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sw_lake_charles);
    sw_lake_charles_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    sw_iowa_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sw_iowa);
    sw_iowa_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    sc_cit_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sc_cities);
    sc_cit_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    sc_lafayette_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sc_lafayette);
    sc_lafayette_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    sc_gueydan_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sc_gueydan);
    sc_gueydan_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);


    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {


                 String reg_select = (sp1.getSelectedItem().toString());
                 if (reg_select.contentEquals("Southwest")){
                     sp2.setAdapter(sw_cit_adp); 
                     sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                    int arg2, long arg3) {
                                // TODO Auto-generated method stub

                             String sw_cit_select = (sp2.getSelectedItem().toString());
                             if (sw_cit_select.contentEquals("Lake Charles")){
                                 sp3.setAdapter(sw_lake_charles_adp); }

                             else if (sw_cit_select.contentEquals("Iowa")){
                                 sp3.setAdapter(sw_iowa_adp); }


                            ;}

                            public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub

                            };});}

                 else  if (reg_select.contentEquals("South Central")){
                     sp2.setAdapter(sc_cit_adp); 
                     sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                    int arg2, long arg3) {
                                // TODO Auto-generated method stub

                             String sc_cit_select = (sp2.getSelectedItem().toString());
                             if (sc_cit_select.contentEquals("Lafayette")){
                                 sp3.setAdapter(sc_lafayette_adp); }

                             else if (sc_cit_select.contentEquals("Gueydan")){
                                 sp3.setAdapter(sc_gueydan_adp); }


                            ;}

                            public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub

                            };});}

                 }



    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});}}
4

1 回答 1

2

那是因为您试图从 Anonymous 内部类内部访问sw_cit_adp 。这些类只喜欢当他们访问的数据成员是最终的 确保你有类似的东西

final ArrayAdapter sw_cit_adp = new ArrayAdapter();

你的变量是最终的。

于 2012-08-05T21:08:05.940 回答