如何将ArrayList实现为Spinner?

时间:2014-11-15 00:54:03

标签: java android arraylist jsoup android-spinner

帮助我解决整件事情的任何问题。 我想用ArrayLists制作一个Spinner,但它根本不起作用! 我认为它与“受保护的空白”有关。部分。 我已经尝试过这么努力,但总是得到同样的错误。

我的主要课程:

package com.example.hvt;

import java.util.ArrayList;

import com.example.hvt.Klassenvertretung.Bezeichnung;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;



public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {

public Spinner news, klassen;
public Button btnnews, btnklassen, btnzurueck1, btnzurueck2;
Bezeichnung bezeichnung;


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

    ArrayList<String> Bezeichnung = new ArrayList<String>();

    String listString = "";

    for (String s : Bezeichnung)
    {
        listString += s + "\t";
    }
    int listInt;
    listInt = Integer.parseInt(listString);

    Spinner klassen;
    ArrayAdapter<String> klassenadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
    klassen = (Spinner) findViewById(R.id.klassenspinner);
    bezeichnung.Hinzufügen(bezeichnung);
    ArrayAdapter<String> klassen_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listInt);
    klassen_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    klassen.setAdapter(klassenadapter);
    klassen.setSelection(0);
    klassen.setOnItemSelectedListener(this);

}


@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
        long id) {
    if (pos == 1){
        setContentView(R.layout.activity_5aklassen);
    }
    if (pos <= 1 ){
        Toast.makeText(parent.getContext(), 
                "Deine Auswahl ist: " + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_LONG).show();
    }
}

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

}
}

同一个包中的一个类(这个类从Internet上选择信息|它应该可以工作,因为我在普通的JAVA中测试过它)

package com.example.hvt;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


public class Vertretungsplan {

public Vertretungsplan(String woche)
{
    Woche = woche;
    Einlesen(woche);
}

public String Woche;

public List<Klassenvertretung> Klassen = new ArrayList<Klassenvertretung>();

private void Hinzufügen(Klassenvertretung neuesElement)
{
    Klassen.add(neuesElement);
}

private void Einlesen(String woche)
{
    try
    {
        for (int webseite = 1; webseite < 10000; webseite++)
        {
            Klassenvertretung klassenvertretung = new Klassenvertretung();

            String teilseite = "w000";
            if (webseite < 10)
                teilseite = teilseite + "0";
            teilseite = teilseite + webseite;

            Document doc =      Jsoup.connect("http://www.hh.shuttle.de/hh/gyhr/Vertretungsplan/Vertretungsplan_Internet/" + woche +    "/w/" + teilseite + ".htm").get();

            Element h2 = doc.select("h2").get(0);


            klassenvertretung.Bezeichnung = h2.text();

            Element table = doc.select("table").get(1);
            Element[] elemente = table.select("tr").toArray(new Element[0]);
            for (int i = 1; i < elemente.length; i++)
            {
                Element[] tds = elemente[i].select("td").toArray(new Element[0]);

                Vertretung vertretung = new Vertretung();
                vertretung.Klasse = tds[0].text();
                vertretung.Stunde = tds[1].text();
                vertretung.Art = tds[2].text();
                vertretung.Fach = tds[3].text();
                vertretung.Raum = tds[4].text();
                vertretung.stattFach = tds[5].text();
                vertretung.stattRaum = tds[6].text();
                vertretung.Informationen = tds[7].text();
                klassenvertretung.Hinzufügen(vertretung);
            }

            Hinzufügen(klassenvertretung);
        }
    }
    catch (IOException io)
    {

    }
    finally
    {
    }
}
}

同一个包中的另一个类(这个代码应该将所有信息组合成两个ArrayLists(在正常的JAVA中测试它,所以不应该是一个错误))

package com.example.hvt;

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



public class Klassenvertretung
{   
public String Bezeichnung;

public static class Bezeichnung {
    public List<Bezeichnung> Bezeichnung = new ArrayList<Bezeichnung>();
    public void Hinzufügen(Bezeichnung neuesElement)
    {

        Bezeichnung.add(neuesElement);
    }


}

public List<Vertretung> Vertretungen = new ArrayList<Vertretung>();

public void Hinzufügen(Vertretung neuesElement)
{
    Vertretungen.add(neuesElement);
}
}

我的上一课:(你可以看到这只是我定义了一些对象的类(它们属于上面的类))

package com.example.hvt;


public class Vertretung
{
public String Klasse;
public String Stunde;
public String Art;
public String Fach;
public String Raum;
public String stattFach;
public String stattRaum;
public String Informationen;
}

聊天记录:

11-15 00:26:26.160: I/dalvikvm(714): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 00:26:26.160: W/dalvikvm(714): VFY: unable to resolve virtual method 11377: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
11-15 00:26:26.160: D/dalvikvm(714): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:26.160: I/dalvikvm(714): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
11-15 00:26:26.160: W/dalvikvm(714): VFY: unable to resolve virtual method 11383: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
11-15 00:26:26.160: D/dalvikvm(714): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:26.170: I/dalvikvm(714): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
11-15 00:26:26.170: W/dalvikvm(714): VFY: unable to resolve virtual method 8956: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
11-15 00:26:26.170: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x000e
11-15 00:26:26.190: I/dalvikvm(714): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 00:26:26.190: W/dalvikvm(714): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 00:26:26.190: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:26.190: I/dalvikvm(714): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 00:26:26.190: W/dalvikvm(714): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
11-15 00:26:26.190: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:26.320: D/AndroidRuntime(714): Shutting down VM
11-15 00:26:26.320: W/dalvikvm(714): threadid=1: thread exiting with uncaught exception (group=0xb1a90d70)
11-15 00:26:26.330: E/AndroidRuntime(714): FATAL EXCEPTION: main
11-15 00:26:26.330: E/AndroidRuntime(714): Process: com.example.hvt, PID: 714
11-15 00:26:26.330: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hvt/com.example.hvt.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.os.Looper.loop(Looper.java:136)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.ActivityThread.main(ActivityThread.java:5026)
11-15 00:26:26.330: E/AndroidRuntime(714):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:26:26.330: E/AndroidRuntime(714):  at java.lang.reflect.Method.invoke(Method.java:515)
11-15 00:26:26.330: E/AndroidRuntime(714):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 00:26:26.330: E/AndroidRuntime(714):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-15 00:26:26.330: E/AndroidRuntime(714):  at dalvik.system.NativeStart.main(Native Method)
11-15 00:26:26.330: E/AndroidRuntime(714): Caused by: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:26.330: E/AndroidRuntime(714):  at java.lang.Integer.invalidInt(Integer.java:137)
11-15 00:26:26.330: E/AndroidRuntime(714):  at java.lang.Integer.parseInt(Integer.java:358)
11-15 00:26:26.330: E/AndroidRuntime(714):  at java.lang.Integer.parseInt(Integer.java:331)
11-15 00:26:26.330: E/AndroidRuntime(714):  at com.example.hvt.MainActivity.onCreate(MainActivity.java:39)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.Activity.performCreate(Activity.java:5242)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 00:26:26.330: E/AndroidRuntime(714):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-15 00:26:26.330: E/AndroidRuntime(714):  ... 11 more
11-15 00:26:29.320: I/Process(714): Sending signal. PID: 714 SIG: 9
11-15 00:26:38.220: I/dalvikvm(803): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 00:26:38.220: W/dalvikvm(803): VFY: unable to resolve virtual method 11377: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
11-15 00:26:38.220: D/dalvikvm(803): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:38.220: I/dalvikvm(803): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
11-15 00:26:38.220: W/dalvikvm(803): VFY: unable to resolve virtual method 11383: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
11-15 00:26:38.230: D/dalvikvm(803): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:38.250: I/dalvikvm(803): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
11-15 00:26:38.250: W/dalvikvm(803): VFY: unable to resolve virtual method 8956: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
11-15 00:26:38.250: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x000e
11-15 00:26:38.270: I/dalvikvm(803): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 00:26:38.270: W/dalvikvm(803): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 00:26:38.270: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:38.280: I/dalvikvm(803): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 00:26:38.280: W/dalvikvm(803): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
11-15 00:26:38.280: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:38.400: D/AndroidRuntime(803): Shutting down VM
11-15 00:26:38.400: W/dalvikvm(803): threadid=1: thread exiting with uncaught exception (group=0xb1a90d70)
11-15 00:26:38.410: E/AndroidRuntime(803): FATAL EXCEPTION: main
11-15 00:26:38.410: E/AndroidRuntime(803): Process: com.example.hvt, PID: 803
11-15 00:26:38.410: E/AndroidRuntime(803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hvt/com.example.hvt.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.os.Looper.loop(Looper.java:136)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.ActivityThread.main(ActivityThread.java:5026)
11-15 00:26:38.410: E/AndroidRuntime(803):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:26:38.410: E/AndroidRuntime(803):  at java.lang.reflect.Method.invoke(Method.java:515)
11-15 00:26:38.410: E/AndroidRuntime(803):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 00:26:38.410: E/AndroidRuntime(803):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-15 00:26:38.410: E/AndroidRuntime(803):  at dalvik.system.NativeStart.main(Native Method)
11-15 00:26:38.410: E/AndroidRuntime(803): Caused by: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:38.410: E/AndroidRuntime(803):  at java.lang.Integer.invalidInt(Integer.java:137)
11-15 00:26:38.410: E/AndroidRuntime(803):  at java.lang.Integer.parseInt(Integer.java:358)
11-15 00:26:38.410: E/AndroidRuntime(803):  at java.lang.Integer.parseInt(Integer.java:331)
11-15 00:26:38.410: E/AndroidRuntime(803):  at com.example.hvt.MainActivity.onCreate(MainActivity.java:39)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.Activity.performCreate(Activity.java:5242)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 00:26:38.410: E/AndroidRuntime(803):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-15 00:26:38.410: E/AndroidRuntime(803):  ... 11 more

1 个答案:

答案 0 :(得分:0)

错误是bcz“无处可解析!!”

你正在创建ArrayList但是根本没有任何值或者向List添加任何值.. !! 当你的List到达for循环时它是空的:所以它将返回运行时异常,如Logcat所示。!

 ArrayList<String> Bezeichnung = new ArrayList<String>();

// populate your List before using it, otherwise it is null/"";
// need some statements like    
// Bezeichnung.add("string1");
// Bezeichnung.add("string1");

    String listString = "";


    for (String s : Bezeichnung)
    {
        listString += s + "\t";
    }

也有一些建议,

您必须遵循Java命名约定以避免命名冲突的错误! 确保在运行之前将它们命名为正确,这可能会导致产生一些意想不到的结果,您可能无法轻易搞清楚!