2016-08-14 4 views
1

私が参照しているTextViewが正しいことを確認し、アプリケーションにインターネット権限があることを確認しました。AsyncTaskを使用するとアプリケーションがクラッシュする

Bitinの現在の価格をCoinbaseのAPIから引き出し、TextViewに表示するアプリを作成しようとしています。 APIとやりとりするためのコードは、私が書いたデスクトップJavaプログラムから正確にコピーされ、価格を取得してデータベースに格納しました。今のところ一週間ほど走っていない。

起動時にアプリがクラッシュします。ここでの唯一のJavaコードは次のとおりです。

import android.app.*; 
import android.os.*; 
import android.widget.*; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.*; 

public class MainActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  
     String price = ""; 
     TextView tester = (TextView) findViewById(R.id.ticker); 
     tester.setText("new text");//This is not displayed before crash 
     TickerTask ticker = new TickerTask(); 
     ticker.execute(); 

    } 


     private class TickerTask extends AsyncTask<Void, Void, Void> { 
      protected Void doInBackground(Void... nothing) { 
       String coinbase = "https://api.coinbase.com/v2/prices/spot?currency=USD"; 
       int i = 0; 
       int y = 0; 
       String price = ""; 
       String formatted_price; 
       TextView ticker = (TextView) findViewById(R.id.ticker); 
        try { 
         URL url = new URL(coinbase); 
         HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

         BufferedReader in = new BufferedReader (
         new  InputStreamReader(connection.getInputStream())); 
         String urlString = ""; 
         String current; 

         while ((current = in.readLine()) != null) { 
          urlString += current;      
         } 
         int begin = urlString.indexOf("amount"); 
         int end = urlString.indexOf("currency"); 
         price = urlString.substring(begin+9, end-3); 

         ticker.setText(price); 

        } catch (Exception e) { 
         ticker.setText(e.getMessage()); 
         }  
        y++; 
        return nothing[0]; 
        }//End of doInBackground 
     }//End of TickerTask 

} 
+3

を見て、あなたのスタックトレースを表示 – GreyBeardedGeek

+0

範囲外の指数は... '何も返さない[0];例外が発生したときに' –

+1

は常にスタックトレースをログに記録し、そうでない場合は、あなたが知っていることは決してないだろうと我々は助けることはできません。 – m0skit0

答えて

1

問題は、あなたがdoInBackgroundのUI(メソッド)を変更することはできません、あなたはなぜそれがエラーを与えているだticker.setText()と呼ばれるprivate class TickerTask extends AsyncTask<String, String, String>をお試しください。 onPreExecute()メソッドを使用して変数を初期化し、onPostExecute()メソッドを使用して、バックグラウンドタスクが完了した後にタスクを実行します。

ここでコードを更新していますが、うまくいきます。 here to get idea about lifecycle of asyctask

class TickerTask extends AsyncTask<Void, Void, String> { 
    String coinbase; 
    int i, y; 
    String price, formatted_price; 
    TextView ticker; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     coinbase = "https://api.coinbase.com/v2/prices/spot?currency=USD"; 
     i = 0; 
     y = 0; 
     price = ""; 
     ticker = (TextView) findViewById(R.id.ticker); 
    } 

    protected String doInBackground(Void... nothing) { 
     try { 
      URL url = new URL(coinbase); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

      BufferedReader in = new BufferedReader(
        new InputStreamReader(connection.getInputStream())); 
      String urlString = ""; 
      String current; 

      while ((current = in.readLine()) != null) { 
       urlString += current; 
      } 

      return urlString; 


     } catch (Exception e) { 
      return "error" 
      e.printStackTrace(); 
     } 
//  return nothing[0]; 
      return "error"; 
    }//End of doInBackground 

    @Override 
    protected void onPostExecute(String urlString) { 
     super.onPostExecute(urlString); 
     if(!urlString.equal("error")) { 
      int begin = urlString.indexOf("amount"); 
      int end = urlString.indexOf("currency"); 
      price = urlString.substring(begin + 9, end - 3); 
      ticker.setText(price); 
     } else 
      ticker.setText("Error"); 
     y++; 
    } 
}//End of TickerTask 
2

doInBackgroundUIに触れることができません。あなたがそれらのコールを行う必要がある場合(あなたのケースではsetText())、 'onPreExecute()'またはonPostExecute()で行います。

+0

何も空ではないので、何も[0]の代わりにnullを返す必要があります – GreyBeardedGeek

+0

ああ...彼は 'execute()'を呼んでいるので – Shaishav

0

の代わりにprivate class TickerTask extends AsyncTask<Void, Void, Void>

関連する問題