我的第一个Android应用程序已经基本完成,这是一个基本的小费计算器。我在36号线遇到问题

amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView);

我收到这些错误:
Multiple markers at this line
-Syntax error, insert ";" to complete FieldDeclaration

-Syntax error on token ".", ... expected

-Syntax error on token "amountDisplayTextView", VariableDeclaratorId expected after this token

-Return type for the method is missing

-Syntax error on token ")", { expected after this token

-Syntax error on token "amountDisplayTextView", VariableDeclaratorId expected after this token

我曾尝试麻烦射击,但碰壁了。任何帮助表示赞赏!这是类(class)的其余部分。
package com.example.tipcalc;

import java.text.NumberFormat;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

      //currency and percent formatters
    private static final NumberFormat currencyFormat =
            NumberFormat.getCurrencyInstance();
    private static final NumberFormat percentFormat =
            NumberFormat.getPercentInstance();

    private double billAmount = 0.0; //bill amount entered by the user
    private double customPercent = 0.18; //initial custom percent value
    private TextView amountDisplayTextView; //shows formatted bill amount
    private TextView percentCustomTextView;//shows custom tip percentage
    private TextView tip15TextView; // shows 15% tip
    private TextView total15TextView; // shows total with 15% tip
    private TextView tipCustomTextView; // shows custom tip amount
    private TextView totalCustomTextView; //shows total with custom tip
  //called when activity is first created
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); //call superclass's version
        setContentView(R.layout.activity_main); //inflate GUI
    }

    //get references to the TextViews
    //that MainActivity interacts with programmatically
    amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView);
    percentCustomTextView = (TextView) findViewById(R.id.percentCustomTextView);
    tip15TextView = (TextView) findViewById(R.id.tip15TextView);
    total15TextView = (TextView) findViewById(R.id.total15TextView);
    tipCustomTextView = (TextView) findViewById(R.id.tipCustomTextView);
    totalCustomTextView = (TextView) findViewById(R.id.totalCustomTextView);
}
    //update 15% textviews
    private void updateStandard()
    {
        //calculate 15% tip and total
        double fifteenPercentTip = billAmount * 0.15;
        double fifteenPercentTotal = billAmount + fifteenPercentTip;

        //display 15% tip and total formatted as currency
        tip15TextView.setText(currencyFormat.format(fifteenPercentTip));
        total15TextView.setText(currencyFormat.format(fifteenPercentTotal));
    } //end method updateStandard

    //updates the custom tip and total TextViews
    private void updateCustom()
    {
        //show customPercent in percentCustomTextView formatted as %
        percentCustomTextView.setText(percentFormat.format(customPercent));

        //calculate the custom tip and total
        double customTip = billAmount * customPercent;
        double customTotal = billAmount + customTip;

        //display custom tip and total formatted as currency
        tipCustomTextView.setText(currencyFormat.format(customTip));
        totalCustomTextView.setText(currencyFormat.format(customTotal));
    }//end updateCustom

    //called when the user changes the position of SeekBar
    private OnSeekBarChangeListener customSeekBarListener =
            new OnSeekBarChangeListener()
    {
        //update customPercent, then call updateCustom
        @Override
        publicvoid onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            //set customPercent to position of the SeekBar's thumb
            customPercent = progress / 100.0; //update the custom tip TextViews
            updateCustom(); //update the custom tip TextView's
    }; //end method onProgressChanged

    @Override
    public void onStartTrackingTouch(SeekBar seekBar)
    {
    }// end method onStartTrackingTouch

    @Override
    public void onStopTrackingTouch(SeekBar seekBar)
    {
    }// end method onStopTrackingTouch
    };//end OnSeekBarChangeListener

    //event-handling object that responds to amountEditText's events
    private TextWatcher amountEditTextWatcher = new TextWatcher()
    {
        //called when the user enters a number
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            //convert amountEditText's text to a double
            try
            {
                billAmount = Double.parseDouble(s.toString()) / 100.0;
            } //end try
            catch (NumberFormatException e)
            {
                billAmount = 0.0; //default if an exception occurs
            }//end catch
            //display currency formatted bill amount
            amountDisplayTextView.setText(currencyFormat.format(billAmount));
            updateStandard(); //update the 15% tip Textviews
            updateCustom(); //update the custom tip TextViews
            }; //end method onTextChanged

            @Override
            public void afterTextChanged(Editable s)
            {
            }//end method afterTextChanged

            @Override
            public void beforeTextChanged (CharSequence s, int start, int count, int after)
            {
            } // end method before TextChanged

            }; //end amountEditTextWatcher

}//end mainActivity class

最佳答案

} {} {}的全部内容是您在类级别上声明变量,并尝试将它们实例化(如果您在onCreate内将其实例化的地方也可以)

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); //call superclass's version
        setContentView(R.layout.activity_main); //inflate GUI
    //get references to the TextViews
    //that MainActivity interacts with programmatically
    amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView);
    percentCustomTextView = (TextView) findViewById(R.id.percentCustomTextView);
    tip15TextView = (TextView) findViewById(R.id.tip15TextView);
    total15TextView = (TextView) findViewById(R.id.total15TextView);
    tipCustomTextView = (TextView) findViewById(R.id.tipCustomTextView);
    totalCustomTextView = (TextView) findViewById(R.id.totalCustomTextView);
    }

关于android - 一行代码上有许多错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24027661/

10-12 00:24
查看更多