Java parseInt和viewflipper布局问题

Java parseInt和viewflipper布局问题,java,android,xml-parsing,viewflipper,parseint,Java,Android,Xml Parsing,Viewflipper,Parseint,我的parseInt()有问题,它抛出错误:无法将“null”解析为整数,而且我的ViewFlipper也不工作。以下是我的活动: import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import android.app.Ac

我的
parseInt()
有问题,它抛出错误:无法将“null”解析为整数,而且我的
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();
     }  

    }       
这是我的处理程序:

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
和handler,我只粘贴了涉及
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>

现在,您正试图将其解析为
int
字符串
null
(您正在从
dataset
调用静态方法,该方法将返回
null
,因为它返回的对象(
temp
)设置为
null
)。您可能希望将xml解析的结果解析为
int
,因此不要:

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)替换活动的主布局

检查您的
parseddataset
是否为空,如果为空,请在字符串的from中添加“0”,然后将其作为整数进行赋值

可按以下方式实施:

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

我建议使用
boolean
变量,如果它的结果为false(!=0),则只解析它,否则您知道它的parseddataset为空。在您使用
xml.parser.dataset.getTemp()的情况下
检查其长度是否为0,如果为false,则进行解析。

@user1234167如果您的代码
parsedDataSet
dataset
的一个实例,则有以下行:
dataset parsedDataSet=myHandler.getParsedData()所以调用该方法应该不会有问题。在输入
try catch
块之前,先声明您的
parsedDataSet
,然后在该块中只将结果分配给
parsedDataSet
。您能给我一个示例,说明我是如何声明我真的感到困惑的吗?我已经做出了更改,但仍然收到相同的错误:(至于将主布局替换为文本视图……为指出这一点,我将把它改为setText或something@user1234167您是否还修改了处理程序中的
characters
方法来设置文本
myParsedDataSet.settep(settep);
?@user1234167您可能希望从
视图翻转器(使用
findViewById
)中获取一个
文本视图
,并在其上设置天气文本(您解析的)。事实上,我刚刚更改了这个:静态字符串temp=“0”;它现在上升为0度,很明显,它现在将显示0,因为你将temp设置为static,等于“0”,只要做这个更改
String temp=0
,如果它将从解析器中读取任何内容,那么
0
将被
解析值
替换,否则它将仅为
0
。因此,如果你仍然得到
0
这意味着文件没有被解析。很抱歉,我没有注意到之前的temp声明,否则会给出将字符串temp初始化为0的答案。无论如何,这个
String temp=0
应该可以解决这个问题。注意:我现在已经让它工作了,我仍然将它声明为静态字符串,但是…它将我的数据作为co读取lor已经根据温度而改变。我的布局现在已经出现。现在的问题是我的数据已经消失了
// 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;
          }
String checknull=parseddataset.toString();
if(checknull.length()==0)
  checknull += "0";