2017-03-21 3 views
0

私はプログラミングがとても新しいです。お互いに連続して走る方法がたくさんあるプログラムがあります。私のアプリケーションは、計算が完了するまでフリーズします。私はすべてをAsyncTaskに入れたいと思っていますが、実際の動作を理解するのは非常に困難です。SImple AsyncTask intを返します

これは私が今まで別々のJavaファイルとして持っているものです。

import android.os.AsyncTask; 

public class ConvertToStringToIntt extends AsyncTask<String, Void, Integer> { 

    @Override 
    protected Integer doInBackground(String... timeString) { 
     String time = timeString; 
     time = time.replace(":",""); 
     int value = -2; 
     try { 
      value = Integer.parseInt(time); 
     } catch (NumberFormatException e) { 
      Message.message(this, "String time could not be converted to int"); 
     } 
     return value; 
    } 
} 

このメソッドは基本的に時間の文字列を受け取り、intとして返します。しかし、私はString time = timeStringにエラーが発生しています。

氏は述べています:必須

互換性のないタイプは:実測java.lang.Stringで: java.lang.Stringで[]

私はそれが何を意味するのか分かりません。どんな助けも素晴らしいだろうし、ありがとう。あなたのアプローチで

+2

timeStringは、文字列の配列であるにかかわらず、ビジネスロジックのあなただけの文字列//を渡すことを確認している場合time = timeString [0]; –

+0

'String ...'の構文についてはhttp://stackoverflow.com/questions/3158730/java-3-dots-in-parametersを参照してください。 – GertG

答えて

0

、とあなたは[文字列を使用し、AsyncTaskを実行すると1つの文字列を送信する場合

public class ConvertToStringToIntt extends AsyncTask<String, Void, Integer> { 

    @Override 
    protected Integer doInBackground(String... timeString) { 
     String time = timeString[0]; 
     time = time.replace(":",""); 
     int value = -2; 
     try { 
      value = Integer.parseInt(time); 
     } catch (NumberFormatException e) { 
      Message.message(this, "String time could not be converted to int"); 
     } 
     return value; 
    } 
} 
+0

私はそれを得るdoInBackground配列を設定します。 int値は、常に単純なintを返します。私は現在MainActivityのAsyncTaskにアクセスしていますが、どうすればそのintをMainActivityの変数に入れることができますか? ConvertToString convertToString =新しいConvertToString(); int i = convertToString.execute(getTimeString);動作しません。 –

関連する問題