2012-04-01 85 views
0

我有一個問題parseInt(),它拋出的錯誤:無法解析「空」爲整數,也是我ViewFlipper不工作。這裏是我的活動:parseInt函數和viewflipper佈局問題

import javax.xml.parsers.SAXParser; 
    import javax.xml.parsers.SAXParserFactory; 

    import org.xml.sax.InputSource; 
    import org.xml.sax.XMLReader; 

    import android.app.Activity; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.LinearLayout; 
    import android.widget.TextView; 
    import android.widget.ViewFlipper; 
    import xml.parser.dataset; 



    public class XmlParserActivity extends Activity implements OnClickListener { 


    private final String MY_DEBUG_TAG = "WeatherForcaster"; 
    // private dataset myDataSet; 
    private LinearLayout layout; 
    private int temp= 0; 

    /** Called when the activity is first created. */ 
    //the ViewSwitcher 
    private Button btn; 
    private ViewFlipper flip; 
// private TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      layout=(LinearLayout)findViewById(R.id.linearlayout1); 
      btn=(Button)findViewById(R.id.btn); 
      btn.setOnClickListener(this); 
      flip=(ViewFlipper)findViewById(R.id.flip);        
      //when a view is displayed 
      flip.setInAnimation(this,android.R.anim.fade_in); 
      //when a view disappears 
      flip.setOutAnimation(this, android.R.anim.fade_out); 
     // String postcode = null; 

     // public String getPostcode { 
      //  return postcode; 
    // } 

        //URL newUrl = c; 

       // myweather.setText(c.toString()); 


      /* Create a new TextView to display the parsingresult later. */ 
      TextView tv = new TextView(this); 





      // run(0); 
      //WeatherApplicationActivity postcode = new WeatherApplicationActivity(); 
      try { 

        /* Create a URL we want to load some xml-data from. */ 
       URL url = new URL("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=G41"); 

       //String url = new String("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query="+WeatherApplicationActivity.postcode); 



       //URL url = new URL(url); 
        //url.toString(); 
        //myString(url.toString() + WeatherApplicationActivity.getString(postcode)); 
        // url + WeatherApplicationActivity.getString(postcode); 
        /* Get a SAXParser from the SAXPArserFactory. */ 
        SAXParserFactory spf = SAXParserFactory.newInstance(); 
        SAXParser sp = spf.newSAXParser(); 

        /* Get the XMLReader of the SAXParser we created. */ 
        XMLReader xr = sp.getXMLReader(); 
        /* Create a new ContentHandler and apply it to the XML-Reader*/ 
        handler myHandler = new handler(); 
        xr.setContentHandler(myHandler); 

        /* Parse the xml-data from our URL. */ 
        xr.parse(new InputSource(url.openStream())); 
        /* Parsing has finished. */ 

        /* Our ExampleHandler now provides the parsed data to us. */ 
        dataset parsedDataSet = 
                    myHandler.getParsedData(); 

        /* Set the result to be displayed in our GUI. */ 
        tv.setText(parsedDataSet.toString()); 


      } catch (Exception e) { 
        /* Display any Error to the GUI. */ 
        tv.setText("Error: " + e.getMessage()); 
        Log.e(MY_DEBUG_TAG, "WeatherQueryError", e); 
      } 



      temp = Integer.parseInt(xml.parser.dataset.getTemp()); 


       if(temp <0){ 
        //layout.setBackgroundColor(Color.BLUE); 
        //layout.setBackgroundColor(getResources().getColor(R.color.silver)); 
        findViewById(R.id.flip).setBackgroundColor(Color.BLUE); 
       } 
       else if(temp > 0 && temp < 9) 
       { 
        //layout.setBackgroundColor(Color.GREEN); 
        //layout.setBackgroundColor(getResources().getColor(R.color.silver)); 
        findViewById(R.id.flip).setBackgroundColor(Color.GREEN); 
       } 
       else 
       { 
        //layout.setBackgroundColor(Color.YELLOW); 
        //layout.setBackgroundColor(getResources().getColor(R.color.silver)); 
        findViewById(R.id.flip).setBackgroundColor(Color.YELLOW); 
       } 


     /* Display the TextView. */ 
    this.setContentView(tv); 
    } 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub onClick(View arg0) { 

     // TODO Auto-generated method stub 
     flip.showNext(); 
     //specify flipping interval    
     //flip.setFlipInterval(1000);   
     //flip.startFlipping(); 
    } 

    }  

這是我dataset

package xml.parser; 



public class dataset { 

static String temp = null; 

// private int extractedInt = 0; 


public static String getTemp() { 
    return temp; 
} 
public void setTemp(String temp) { 
    this.temp = temp; 
} 

這是我的處理程序:

public void characters(char ch[], int start, int length) { 
if(this.in_temp){ 
      String setTemp = new String(ch, start, length); 
      // myParsedDataSet.setTempUnit(new String(ch, start, length)); 
     // myParsedDataSet.setTemp; 
      } 

對於dataset和處理程序,我只粘貼,涉及到temp代碼因爲我知道他們在我拿出if語句時工作。但即使如此,我的ViewFlipper不會工作。 這是我main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/linearlayout1" 
> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25dip" 
    android:text="Flip Example" 
/> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="25dip" 
     android:id="@+id/tv" 
    /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="25dip" 
    android:text="Flip" 
    android:id="@+id/btn" 
    android:onClick="ClickHandler" 
/> 
<ViewFlipper 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/flip"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="25dip" 
      android:text="Item1a" 
     /> 


    </LinearLayout> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="25dip" 
     android:id="@+id/tv2" 
    /> 

</ViewFlipper> 
</LinearLayout> 

這是我Logcat

04-01 18:02:24.744: E/AndroidRuntime(7331): FATAL EXCEPTION: main 
04-01 18:02:24.744: E/AndroidRuntime(7331): java.lang.RuntimeException: Unable to start activity ComponentInfo{xml.parser/xml.parser.XmlParserActivity}: java.lang.NumberFormatException: unable to parse 'null' as integer 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.os.Looper.loop(Looper.java:150) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.ActivityThread.main(ActivityThread.java:4293) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at dalvik.system.NativeStart.main(Native Method) 
04-01 18:02:24.744: E/AndroidRuntime(7331): Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at java.lang.Integer.parseInt(Integer.java:356) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at java.lang.Integer.parseInt(Integer.java:332) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at xml.parser.XmlParserActivity.onCreate(XmlParserActivity.java:118) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
04-01 18:02:24.744: E/AndroidRuntime(7331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794) 

回答

1

現在你試圖解析爲int一個null字符串(您呼叫從dataset將在靜態方法因爲它返回的對象(temp)被設置爲null)你可能想解析爲int XML解析的結果,所以不是:

temp = Integer.parseInt(xml.parser.dataset.getTemp()); 

使用parsedDataSet變量:

temp = Integer.parseInt(parsedDataSet.getTemp()); 

例子:

//.... 
    dataset parsedDataSet; 
    try { 
     URL url = new URL(
       "http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=G41"); 
     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     SAXParser sp = spf.newSAXParser(); 
     XMLReader xr = sp.getXMLReader(); 
     handler myHandler = new handler(); 
     xr.setContentHandler(myHandler); 
     xr.parse(new InputSource(url.openStream())); 
     parsedDataSet = myHandler.getParsedData(); 
     tv.setText(parsedDataSet.toString()); 
    } catch (Exception e) { 
     tv.setText("Error: " + e.getMessage()); 
     Log.e(MY_DEBUG_TAG, "WeatherQueryError", e); 
    } 

    temp = Integer.parseInt(parsedDataSet.getTemp()); 
    //... 

我也看你的代碼中的其他錯誤:

- 在xml解析器中,你會實際上在結果中設置瞭解析的字符串?

// I guess that myParsedDataSet is another dataset object that you return with 
// myHandler.getParsedData(), do you set the temp variable in that class?? // myParsedDataSet.setTemp(setTemp); 
if(this.in_temp){ 
      String setTemp = new String(ch, start, length); 
      // myParsedDataSet.setTempUnit(new String(ch, start, length)); 
     // myParsedDataSet.setTemp; 
      } 

- 同樣您解析XML,從parseInt檢查值,然後用this.setContentView(tv);更換活動的主要佈局?!

+0

因爲getTemp()方法在數據集中,所以不起作用... – user1234167 2012-04-01 17:39:44

+0

@ user1234167從你的代碼中''parsedDataSet'是'dataset'的一個實例,你有這樣一行:'dataset parsedDataSet = myHandler.getParsedData() ;所以調用該方法不應該是一個問題。在輸入try-catch塊之前聲明你的'parsedDataSet',並在該塊中只將結果賦值給'parsedDataSet'。 – Luksprog 2012-04-01 17:43:23

+0

你能否給我舉一個例子,說明我真的很困惑 – user1234167 2012-04-01 18:13:53

1

檢查您的parseddatadset是否爲空,如果在將字符串作爲整數進行粘貼之前在其中添加「0」。

可以實現如下 -

String checknull=parseddataset.toString(); 
if(checknull.length()==0) 
    checknull += "0"; 

我會建議使用boolean可變的,如果它導致假(!= 0),那麼僅分析它,否則你就知道它parseddataset是空的。在你使用xml.parser.dataset.getTemp()的情況下,檢查它的長度是否爲0,如果是false,則解析。

+0

嘿,我已經試過了,我仍然得到相同的錯誤 – user1234167 2012-04-01 18:23:42

+0

事實上,我剛剛改變了這一點:static String temp =「0」;並且它現在成爲0度 – user1234167 2012-04-01 18:25:53

+0

顯然現在當你使臨時靜態和等於'0'時只顯示'String temp = 0',並且如果它將從解析器讀取任何東西,則'0'將被「解析的值」替換,否則它將僅爲「0」。所以如果你仍然得到'0',這意味着該文件沒有得到解析。對不起,沒有注意到臨時溫度的聲明,否則會給出初始化字符串溫度0的答案。無論如何,這個'String temp = 0'應該可以解決問題。 :D – Exorcist 2012-04-01 18:47:42