0

我正在解析数据并将其显示在 textview 上。虽然我已经初始化了清单文件中的所有变量和必要的权限。我仍然收到致命错误。请看一下代码。提前致谢。

如果我使用 android 2.1 i.ie API-7 ..但是如果我用 android 4.0.3 ie API-15 替换它,应用程序将成功运行 ..它会抛出相同的 reeor ..正如我在 logcat 文件中提到的

主要活动

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

View layout = findViewById(R.id.layout);


TextView id[];
TextView Name[];
TextView Email[];
TextView Address[];


try{
    SAXParserFactory saxPF= SAXParserFactory.newInstance();
     SAXParser saxP = saxPF.newSAXParser();
     XMLReader xmlR = saxP.getXMLReader();

     URL url = new URL("http://xyz");

     XMLHandler myXMLHandler = new XMLHandler();
     xmlR.setContentHandler(myXMLHandler);
     xmlR.parse(new InputSource(url.openStream()));

}catch(Exception e){
    e.printStackTrace();
}


data= XMLHandler.data;

id = new TextView[data.getId().size()];
Name = new TextView[data.getName().size()];
Email = new TextView[data.getEmail().size()];


for(int i=0 ; i <data.getName().size(); i++)
{
    id[i] = new TextView (this);
    id[i].setText("Id : " + data.getId().get(i));


    Name[i] = new TextView(this);
    Name[i].setText("Name : " + data.getName().get(i));


    Email[i] = new TextView(this);
    Email[i].setText("Email : " + data.getEmail().get(i));


((ViewGroup)layout).addView(id[i]);
((ViewGroup)layout).addView(Name[i]);   
((ViewGroup)layout).addView(Email[i]);


}

XMLGettersSetters 类

public class XMLGettersSetters {

    private ArrayList<String> id = new ArrayList<String>();
    private ArrayList<String> Name = new ArrayList<String>();
    private ArrayList<String> Email = new ArrayList<String>();


    public ArrayList<String> getId(){

        return id;
    }

    public void setId(String id){
        this.id.add(id);
    }



    public ArrayList<String> getName(){
        return Name;
    }

    public void setName(String name)
    {
        this.Name.add(name);
    }



    public ArrayList<String> getEmail(){
        return Email;

    }
    public void setEmail(String email){
        this.Email.add(email);

    }




}

XMLHandler 类

public class XMLHandler extends DefaultHandler {
    String elementValue= null;
    Boolean elementOn = false;
    public static XMLGettersSetters data = null;

    public static XMLGettersSetters getXMLData(){
        return data;
    }

    public static void setXMLData(XMLGettersSetters data)
    {
        XMLHandler.data=data;
    }



    public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException{
        elementOn=true;



        if(localName.equals("ExaReceipts")){
            data = new XMLGettersSetters();

        }else if(localName.equals("Organisation")){
    }
    }

    public void endElement(String uri,String localName,String qName)throws SAXException
    {

        elementOn = false;

        if(localName.equalsIgnoreCase("id"))
            data.setId(elementValue);

        else if(localName.equalsIgnoreCase("Name"))
            data.setName(elementValue);

        else if(localName.equalsIgnoreCase("Email"))
        data.setEmail(elementValue);
    }



    public void characters (char[] ch , int start , int length)throws SAXException
    { 
        if(elementOn){
            elementValue= new String(ch,start,length);
            elementOn=false;
        }

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spinnertest1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
 <uses-permission android:name="android.permission.INTERNET" />"
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SpinnerTest1Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

日志猫

06-26 17:41:38.482: W/dalvikvm(2129): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
06-26 17:41:38.492: E/AndroidRuntime(2129): FATAL EXCEPTION: main 
06-26 17:41:38.492: E/AndroidRuntime(2129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spinnertest1/com.spinnertest1.SpinnerTest1Activity}: java.lang.NullPointerException 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.ActivityThread.access$600(ActivityThread.java:123) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.os.Handler.dispatchMessage(Handler.java:99) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.os.Looper.loop(Looper.java:137) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at java.lang.reflect.Method.invokeNative(Native Method) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at java.lang.reflect.Method.invoke(Method.java:511) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at dalvik.system.NativeStart.main(Native Method)
06-26 17:41:38.492: E/AndroidRuntime(2129): Caused by: java.lang.NullPointerException 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at com.spinnertest1.SpinnerTest1Activity.onCreate(SpinnerTest1Activity.java:58) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.Activity.performCreate(Activity.java:4465) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
06-26 17:41:38.492: E/AndroidRuntime(2129):     ... 11 more
4

1 回答 1

0

你在调用execute()方法吗?

execute(URL);

URL http://exareceipts.exa.com.au/request_queue/invoice_request/get_test_data

您使用的不在 XML 标记中。

于 2012-06-26T12:01:43.483 回答