【问题标题】:Can't readline() without crashing my Android app无法在我的 Android 应用程序崩溃的情况下 readline()
【发布时间】:2011-11-15 01:02:25
【问题描述】:

我正在构建一个应用程序,它接收网站数据并简单地显示它。我让它工作了,但后来我尝试将它实现到一个选项卡式 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);
}
}

【问题讨论】:

  • 您遇到了什么异常或错误?
  • 当我尝试以不同的方式读取数据时,我使用了 try catch,但它一直给我 IOException 错误。我认为可能是 URL 不正确,但事实并非如此。

标签: java android tabs readline


【解决方案1】:

我只能假设scanner 永远不会在WedReader 构造函数中创建。在该构造函数的 catch 块中做一些有用的事情,即使你只是通过 ADB 写出,所以你知道它失败了。

【讨论】:

  • 如果该部分的代码自上次程序以来没有更改,为什么它现在会失败?从字面上看,唯一改变的是标签的使用。
  • 我不知道,但是,请检查您的构造函数是否运行没有任何问题。
【解决方案2】:

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

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

这将导致在输入的最后一行引发java.util.NoSuchElementException 异常。他们以不同的方式对待行进行标记化。

用途:

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

【讨论】:

  • 你的意思是替换单行还是if语句?
  • 替换以return开头的行。
【解决方案3】:
catch (IOException ioe) 
    {

    }

无论问题是什么,它都被您未能执行打印、记录或对此异常执行任何其他有用的操作所掩盖。 从不忽略异常,当我在咆哮时,总是打印或记录异常本身,而不仅仅是您自己编造的错误消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多