2

私のアプリケーションは、金額を入力し、金額を計算して、金額を表示することです。

Android SDK 16を使用しています。金額を入力して計算ボタンをクリックすると、

SPAN_EXCLUSIVE_EXCLUSIVEスパンの長さをゼロにすることはできません。

私のアクティビティクラスは

   public class ChapterTwo extends Activity {

public static final String tag = "Chapter 2";

static{
    StrictMode.ThreadPolicy policy = new     
        StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);
}




@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chapter_two);

    final EditText mealpricefield = (EditText) findViewById(R.id.mealprice);
    final TextView answerfield = (TextView) findViewById(R.id.answer);
    final Button button = (Button) findViewById(R.id.calculate);

            button.setOnClickListener(new Button.OnClickListener() {

            public void onClickView(View v) {
            try {
                Log.i(tag, "onClick Invoked");
                // grab the meal price from UI
                String mealprice = mealpricefield.getText().toString();
                Log.i(tag, "meal price is [" + mealprice + "]");
                String answer = "";
                // check to see if meal price contains $
                if (mealprice.indexOf("$") == -1) {
                    mealprice = "$" + mealprice;
                }

                float fmp = 0.0F;
                // get currency formatter;
                NumberFormat nf = java.text.NumberFormat
                        .getCurrencyInstance();
                // grab the input meal price
                fmp = nf.parse(mealprice).floatValue();
                // let's give a nice tip
                fmp *= 1.2;
                Log.i(tag, " Total meal price Unformatted is [" + fmp + "]");
                // format our result
                answer = " Full Price, Including the 20% Tip is "
                        + nf.format(fmp);
                answerfield.setText(answer);
                Log.i(tag, " On Click Complete");

            } catch (java.text.ParseException e) {

                Log.i(tag, "Parse Exception caught");
                answerfield.setText("Failed to Parse the Exception");
            }

            catch (Exception e) {
                Log.e(tag, "Failed To calculate the tip" + e.getMessage());
                e.printStackTrace();
                answerfield.setText(e.getMessage());

            }

        }

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
}}

私のレイアウトは

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Chapter 2 Android Tip Calculator"
    tools:ignore="HardcodedText" />

<EditText
    android:id="@+id/mealprice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:inputType="text">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/calculate"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate Tip" />

<EditText
    android:id="@+id/answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

ログ猫で次のエラーが発生します

10-23 20:19:26.232: E/Trace(699): error opening trace file: No such file or directory (2)
10-23 20:19:27.092: D/gralloc_goldfish(699): Emulator without GPU emulation detected.
10-23 20:19:33.262: W/IInputConnectionWrapper(699): showStatusIcon on inactive InputConnection
10-23 20:24:22.106: I/Choreographer(699): Skipped 30 frames!  The application may be doing too much work on its main thread.
10-23 20:27:52.824: I/Choreographer(699): Skipped 37 frames!  The application may be doing too much work on its main thread.
10-23 20:31:13.323: E/SpannableStringBuilder(699): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-23 20:31:13.323: E/SpannableStringBuilder(699): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-23 20:31:32.552: I/Choreographer(699): Skipped 35 frames!  The application may be doing too much work on its main thread.
10-23 20:39:44.723: I/Choreographer(699): Skipped 63 frames!  The application may be doing too much work on its main thread.
4

3 に答える 3

1

あなたが投稿したもの以外のコードはありますか?

ここに私が気づいたいくつかのことがあります:

  1. 上記のように onClick(View v) メソッドで何もしていないため、ボタンを押してもコードは何もしません。誰も呼び出さない onClickView(View v) 関数があります。コードを onClickView() から onClick() に移動し、onClickView() メソッドを削除する必要があります (または、onClick 関数から onClickView を呼び出すことができます)。

  2. 上記のようにコードを移動すると、コードが機能します。今、自分でテストして再確認しました。(上記のコピー アンド ペースト コードには </LinearLayout> タグがありませんが、あると思います)。

  3. 投稿したエラー ログは、SpannableStringBuilder からのエラーを示しています。ただし、コードのどこでも SpannableStringBuilder をまったく使用していないため、a) 他のコードがあるか、b) このエラーはコードとは関係ありません。

ボタンを押すとどうなりますか?アプリケーションがクラッシュしますか? 問題が発生したときにテストに使用する値は何ですか? ログにタグまたはアプリケーション名を持つ他のログはありますか? 投稿したログには、強制終了ログやアプリケーションに関連するエラーは表示されません。

よく考えてみると、[計算] ボタンを押しても何も起こらないことが問題だとおっしゃっていますか? エラーが発生したと思い込んで、ログに表示されたエラーがコードが機能しない理由だと思っていませんか? その場合は、onClickView() 内のコード ブロックを onClick() に移動し、onClickView() を削除するだけです。エラー ログは、コードとは関係のない他のプログラムからのものです。

于 2012-10-24T05:21:14.387 に答える
1

すべてのビューをメイン クラスで宣言し、onCreate() メソッドで初期化する必要があります。

public class ChapterTwo extends Activity {

public static final String tag = "Chapter 2";
EditText mealpricefield;
TextView answerfield;
Button button;
.
.
.
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chapter_two);

    mealpricefield = (EditText) findViewById(R.id.mealprice);
    answerfield = (TextView) findViewById(R.id.answer);
    button = (Button) findViewById(R.id.calculate);
.
.
.
}

また、onClickView(View v) に配置したコードは、コードの下にある onClick(View v) 内にある必要があります。

@Override
public void onClick(View v) {
        // TODO Auto-generated method stub

    }
于 2012-10-24T04:25:00.937 に答える
0

一見すると、これはあなたの問題かもしれません。

Log.i(tag, " 総食事代 Unformatted is [" + fmp + "]");

fmp はフロートです...文字列に追加しています。

この行をコメントアウトして、機能するかどうかを確認してください。

于 2012-10-24T04:58:39.823 に答える