不能在不崩溃我的Android应用程序的情况下读取()

时间:2011-11-15 01:02:25

标签: java android tabs readline

我正在构建一个应用程序,它接收网站数据并简单地显示它。我有它工作,但后来我尝试将它实现到一个标签式UI,它现在无法正常工作。

我把它缩小到了

    input = reader.readLine();

这是原因,但我不明白出了什么问题。

主要类

package app.stockup;

import java.util.ArrayList;

import app.stockup.R;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class Main extends TabActivity
{
public static ArrayList<ShareSet> sharePortfolio = new ArrayList<ShareSet>();

Resources resources;
TabHost tabHost;

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

    resources = getResources();
    tabHost = getTabHost();


    buildPortfolio();
    buildPage(Status.class, "Status", R.drawable.status_tab);
    buildPage(Portfolio.class, "Portfolio", R.drawable.portfolio_tab);

    tabHost.setCurrentTab(0);
}

private void buildPage(Class<? extends Activity> className, String tabName, int tabID)
{
    Intent intent = new Intent(this, className);

    tabHost.addTab(tabHost.newTabSpec(tabName)
           .setIndicator(tabName, resources.getDrawable(tabID))
           .setContent(intent));
}

private void buildPortfolio()
{       
    sharePortfolio.add(new ShareSet("BLVN", "Bowleven", 3960, R.id.portfolioTextBLVN, R.id.statusTextBLVN));
    sharePortfolio.add(new ShareSet("BP", "British Petroleum", 192, R.id.portfolioTextBP, R.id.statusTextBP));
    sharePortfolio.add(new ShareSet("EXPN", "Experian", 258, R.id.portfolioTextEXPN, R.id.statusTextEXPN));
    sharePortfolio.add(new ShareSet("HSBA", "HSBC", 343, R.id.portfolioTextHSBC, R.id.statusTextHSBC));
    sharePortfolio.add(new ShareSet("MKS", "Marks & Spencers", 485, R.id.portfolioTextMS, R.id.statusTextMS));
    sharePortfolio.add(new ShareSet("SN", "Smith & Nephew", 1219,     R.id.portfolioTextSN, R.id.statusTextSN));
}
}

状态类

package app.stockup;

import java.util.ArrayList;
import java.util.Iterator;

import app.stockup.Main;
import app.stockup.ShareSet;
import app.stockup.WebReader;
import app.stockup.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class Status extends Activity
{   
private ArrayList<ShareSet> myShares = Main.sharePortfolio;
Iterator<ShareSet> iterator = myShares.iterator();

ShareSet mySet;

TextView connectionStatus, fieldID;

int color;
WebReader reader;

double currentPrice;
double openPrice;
double difference;

String input;
String percentage;
String shareDirection;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.status);
    connectionStatus = (TextView)findViewById(R.id.connectionStatus); 
    connectionStatus.setText("Internet : Connected");
    compareSharePrice();
}

public void compareSharePrice()
{       
    while(iterator.hasNext())
    {
        mySet = iterator.next();
        fieldID = (TextView)findViewById(mySet.getStatusFieldID());
        reader = new WebReader(mySet.getStockURL());
        input = reader.readLine();
        currentPrice = reader.getCurrentPrice(input);
        openPrice = reader.getOpenPrice(input);
        difference = Math.abs(openPrice - currentPrice);
        percentage = String.format("%.2f", 100/(openPrice/difference));

        if(currentPrice >= openPrice)
        {
            color = (int) Color.GREEN;
            shareDirection = "rocketed";
        }

        else if(currentPrice <= openPrice)
        {
            color = (int) Color.RED;
            shareDirection = "plummeted";
        }

        fieldID.setText(mySet.getStockCode() + " has " + shareDirection + " by " + percentage + "%");
        fieldID.setTextColor(color);
    }
}
}

WebReader类

package app.stockup;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class WebReader
{
private static Scanner scanner;

int trade;
int from;
int to;

String price;

public WebReader(URL url)
{
    try
    {
        URLConnection website = url.openConnection();
        InputStream inputStream = website.getInputStream();
        scanner = new Scanner(new BufferedInputStream(inputStream));
    }
    catch (IOException ioe) 
    {

    }
}

public String readLine()
{
    if (scanner.hasNextLine())
    {           
        return scanner.useDelimiter("\\A").next();
    }

    return null;
}

public double getCurrentPrice(String input)
{
    trade = input.indexOf("Last Trade:", 0);
    from = input.indexOf("<b><span", trade); 
    from = input.indexOf(">", from + 4);        
    to = input.indexOf("</span></b>", from);        
    price = input.substring(from + 1, to); 
    return Double.parseDouble(price); 
} 
/*
 * 
 */
public double getOpenPrice(String input)
{
    trade = input.indexOf("Open:", 0);
    from = input.indexOf("><td class", trade);
    from = input.indexOf(">", from + 4);        
    to = input.indexOf("</td></tr>", from);     
    price = input.substring(from + 1, to);
    return Double.parseDouble(price);
}
}

3 个答案:

答案 0 :(得分:0)

我只能假设在scanner构造函数中永远不会创建WedReader。在该构造函数的catch块中使用某些是有用的,即使您只是通过ADB写出来,因此您知道它失败了。

答案 1 :(得分:0)

您正在混合Scanner.hasNextLine()Scanner.next()

if (scanner.hasNextLine())
{           
    return scanner.useDelimiter("\\A").next();
}

这将导致在输入的最后一行抛出java.util.NoSuchElementException异常。他们以不同方式处理行标记化。

使用:

return scanner.useDelimiter("\\A").nextLine();

答案 2 :(得分:-1)

catch (IOException ioe) 
    {

    }

无论出现什么问题,都会因为您未能执行打印,记录或对此异常执行任何其他操作而屏蔽它。 从不忽略异常,当我咆哮时,始终打印或记录异常本身,而不仅仅是您自己的错误消息。