我无法在微调器中

我无法在微调器中

我是android程序员的新手,但有一个问题,我无法在微调器中显示AirNum[]

            String AirNum[]=new String[10];
            int i;

            @SuppressLint("NewApi")
            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            StrictMode.ThreadPolicy policy = new StrictMode.
            ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            String Url = "...url for viewsorce...";

            Document doc = null;
            try
           {
                doc = Jsoup.connect(Url).get();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Elements flightnum = doc.select("p.flightNum");
            for(i=0;i<flightnum.size();i++)
            { AirNum[i] = flightnum.get(i).text();}

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,AirNum);
            sp1.setAdapter(adapter);


这是我的logcat

04-24 00:09:44.638: E/AndroidRuntime(9619): java.lang.NullPointerException
04-24 00:09:44.638: E/AndroidRuntime(9619): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-24 00:09:44.638: E/AndroidRuntime(9619): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-24 00:09:44.638: E/AndroidRuntime(9619): at android.widget.Spinner.measureContentWidth(Spinner.java:681)

最佳答案

您创建一个字符串[]

new String[10];


后,您用flightnum.size()元素填充它。如果这里的元素不完全是10个,它将崩溃(如果元素多于10个,则在setAdapter中崩溃)。

改用列表:

List<String> AirNum = new ArrayList<String>();


并替换:

AirNum[i] = flightnum.get(i).text();


通过

AirNum.add(flightnum.get(i).text());

09-08 02:17