有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java parseInt和viewflipper布局问题

我的parseInt()有问题,它抛出错误:无法将'null'解析为整数,而且我的ViewFlipper也不工作。以下是我的活动:

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

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

    import 安卓.app.Activity;
    import 安卓.graphics.Color;
    import 安卓.os.Bundle;
    import 安卓.util.Log;
    import 安卓.view.View;
    import 安卓.view.View.OnClickListener;
    import 安卓.widget.Button;
    import 安卓.widget.LinearLayout;
    import 安卓.widget.TextView;
    import 安卓.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,安卓.R.anim.fade_in);
            //when a view disappears
            flip.setOutAnimation(this, 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:id="@+id/linearlayout1"
>
<TextView 
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:textSize="25dip"
    安卓:text="Flip Example"
/>
    <TextView 
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:textSize="25dip"
        安卓:id="@+id/tv"
    />
<Button
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:textSize="25dip"
    安卓:text="Flip"
    安卓:id="@+id/btn"
    安卓:onClick="ClickHandler"
/>
<ViewFlipper
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:id="@+id/flip">
    <LinearLayout 
        安卓:orientation="vertical"
        安卓:layout_width="fill_parent"
        安卓:layout_height="fill_parent"
    >
        <TextView 
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:textSize="25dip"
            安卓:text="Item1a"
        />


    </LinearLayout>
    <TextView 
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:textSize="25dip"
        安卓: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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.app.ActivityThread.access$1500(ActivityThread.java:132)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.os.Handler.dispatchMessage(Handler.java:99)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.os.Looper.loop(Looper.java:150)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at com.安卓.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 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-01 18:02:24.744: E/AndroidRuntime(7331):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)

共 (0) 个答案