开发者

Field cannot be resolved? Insert ) and ;?

开发者 https://www.devze.com 2023-03-12 19:00 出处:网络
I made this code in Eclipse (for Android), but there are some errors . Layout: <Button android:layout_width=\"wrap_content\"

I made this code in Eclipse (for Android), but there are some errors .

Layout:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click"
    android:id="@+id/click"
    />

<Edittext
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/meal"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/answerfield"
    />

</LinearLayout>

Main:

package tip.com.tip;


import tip.com.R;开发者_开发问答
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class com extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button button = (Button) findViewById(R.id.click);
        final EditText edittext = (EditText) findViewById(R.id.meal);
        final TextView textView = (TextView) findViewById(R.id.answerfield);

        button.setOnClickListener(new Button.OnClickListener(){
            public void onClick (View v) {
                String meal = edittext.getText().toString();
                String answer = "";

                if (meal.indexOf("$") == -1) {
                    meal = "$" + meal;
                }

                float fmp = 0.0F;
                NumberFormat nf = java.text.NumberFormat.getCurrencyInstance();
                fmp = nf.parse(meal).floatValue();
                fmp *= 1.2;

                answer = " The meal's price is: " + nf.format(fmp);
                answerfield.setText(answer);
            }
         }
}
}

It write to me, that anserfield cannot be resolved, and Syntax error, insert ')' and in an another error insert ';'. What's the problem? I can't find the mistake.


  • Your setOnClickListener is missing a );, e.g. a closing parenthesis and semicolon at the end.

  • Your answerfield is never declared, thanks to @WarrenFaith for the hint.

I was also originally suggesting to try to remove the following line:

    import tip.com.R;

At least I recall once having problems with it. You might want to check for syntax errors in the layout XML file.


It appears you are missing ); from your call to setOnClickListener.

0

精彩评论

暂无评论...
验证码 换一张
取 消