1

我想在异步任务中获取所有城市,然后当用户按下一个城市时,我想在另一个异步任务中获取该城市内的所有区域

我试过这个:

class getAllCities extends AsyncTask<String, Integer, List<City>> {

        @Override
        protected void onPostExecute(List<City> result) {
            super.onPostExecute(result);
            final String newCities[] = new String[result.size()];
            for (int i = 0; i < result.size(); i++)
                newCities[i] = result.get(i).getName();
            cityAdapter = new ArrayAdapter<String>(Address.this,
                    android.R.layout.simple_spinner_item, newCities);
            cityAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s_city.setAdapter(cityAdapter);
            s_city.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    //Log.d("item", newCities[arg2]);
                    new getAreasInCity(newCities[arg2]).execute("d");
                }

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

        @Override
        protected List<City> doInBackground(String... params) {
            return City.getAllCities();
        }
    }

    class getAreasInCity extends AsyncTask<String, Integer, List<Area>> {

        City city;

        public getAreasInCity(String cityName) {
            city = City.getCity(cityName);
        }

        @Override
        protected void onPostExecute(List<Area> result) {
            super.onPostExecute(result);
            if(result == null)
                Log.d("area size = ", "null");
            else
                Log.d("area size = ", result.size()+"");
            String newArea[] = new String[result.size()];
            s_area = (Spinner)findViewById(R.id.s_address_area);
            areaAdapter = new ArrayAdapter<String>(Address.this,
                    android.R.layout.simple_spinner_item, newArea);
            areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            if(s_area != null){
                Log.d("here","saaallllllllllllllllllllll");
                areaAdapter.notifyDataSetChanged();
                s_area.setAdapter(areaAdapter);
            }
            else
                Log.d("here ", "asedsssssssssssssss");
        }

        @Override
        protected List<Area> doInBackground(String... params) {
            return Area.getAreasInCity(city.getID());
        }
    }

在日志猫中我可以看到这个saaallllllllllllllllllllll,我得到了这个异常:

06-07 12:00:18.968: E/AndroidRuntime(25867): FATAL EXCEPTION: main
06-07 12:00:18.968: E/AndroidRuntime(25867): java.lang.NullPointerException
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3231)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1021)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:390)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:313)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1086)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:254)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.ScrollView.onMeasure(ScrollView.java:300)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3231)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:254)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:535)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:313)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3231)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:254)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.View.measure(View.java:8462)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.ViewRoot.performTraversals(ViewRoot.java:861)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1882)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.os.Looper.loop(Looper.java:130)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at android.app.ActivityThread.main(ActivityThread.java:3701)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at java.lang.reflect.Method.invokeNative(Native Method)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at java.lang.reflect.Method.invoke(Method.java:507)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-07 12:00:18.968: E/AndroidRuntime(25867):    at dalvik.system.NativeStart.main(Native Method)

请注意,当我删除s_area.setAdapter(areaAdapter);异常时,新的微调器变为空。

请有任何帮助

我的所有代码

public class Address extends Activity implements OnClickListener {

    Spinner s_city, s_area;
    ArrayAdapter<String> cityAdapter;
    ArrayAdapter<String> areaAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.address);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initialize();
    }

    String[] cities = new String[0];
    String[] areas = new String[0];

    private void initialize() {
        s_city = (Spinner) findViewById(R.id.s_address_city);
        cityAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, cities);
        cityAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s_city.setAdapter(cityAdapter);

        s_area = (Spinner) findViewById(R.id.s_address_area);
        areaAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, areas);
        s_area.setAdapter(areaAdapter);

        new getAllCities().execute("city");
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.b_addItem_save:
            Intent resultsIntent = new Intent();
            resultsIntent.putExtra("City", "Roma");
            setResult(RESULT_OK, resultsIntent);
            finish();
            break;

        default:
            break;
        }
    }

    class getAllCities extends AsyncTask<String, Integer, List<City>> {

        @Override
        protected void onPostExecute(List<City> result) {
            super.onPostExecute(result);
            final String newCities[] = new String[result.size()];
            for (int i = 0; i < result.size(); i++)
                newCities[i] = result.get(i).getName();
            cityAdapter = new ArrayAdapter<String>(Address.this,
                    android.R.layout.simple_spinner_item, newCities);
            cityAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s_city.setAdapter(cityAdapter);
            s_city.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    //Log.d("item", newCities[arg2]);
                    new getAreasInCity(newCities[arg2]).execute("d");
                }

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

        @Override
        protected List<City> doInBackground(String... params) {
            return City.getAllCities();
        }
    }

    class getAreasInCity extends AsyncTask<String, Integer, List<Area>> {

        City city;

        public getAreasInCity(String cityName) {
            city = City.getCity(cityName);
        }

        @Override
        protected void onPostExecute(List<Area> result) {
            super.onPostExecute(result);
            if(result == null)
                Log.d("area size = ", "null");
            else
                Log.d("area size = ", result.size()+"");
            String newArea[] = new String[result.size()];
            for (int i = 0; i < result.size(); i++)
                   newArea[i] = result.get(i).getName();

            areaAdapter = new ArrayAdapter<String>(Address.this,
                    android.R.layout.simple_spinner_item, newArea);
            areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            if(s_area != null){
                Log.d("here","saaallllllllllllllllllllll");
                areaAdapter.notifyDataSetChanged();
                s_area.setAdapter(areaAdapter);

            }
            else
                Log.d("here ", "asedsssssssssssssss");
        }

        @Override
        protected List<Area> doInBackground(String... params) {
            return Area.getAreasInCity(city.getID());
        }
    }
}
4

1 回答 1

2

newArea初始化后忘记在 Array 中添加项目result.size()。这样做:

for (int i = 0; i < result.size(); i++)
   newArea[i] = result.get(i).getName();

并且还使用 Activity Context 来初始化 SpinneronPostExecute而不是getAreasInCity类:

s_area = (Spinner)Address.this.findViewById(R.id.s_address_area);
于 2013-06-06T09:07:50.053 回答