首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一行代码上有许多错误吗?

一行代码上有许多错误吗?
EN

Stack Overflow用户
提问于 2014-06-04 01:35:24
回答 1查看 113关注 0票数 1

我几乎完成了我的第一个android应用程序,这是一个基本的提示计算器。我在第36行遇到麻烦了

代码语言:javascript
复制
amountDisplayTextView = (TextView) findViewById(R.id.amountDisplayTextView); 

我发现了这些错误:

代码语言:javascript
复制
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

我试着去射击,但我撞到了墙。任何帮助都是非常感谢的!这是班上的其他同学。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-04 01:50:50

这都是关于您的{}{}您在类级别上声明了您的变量,如果您在onCreate中实例化它们应该在它们应该在的位置,那么它也试图实例化它们。

代码语言:javascript
复制
   @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);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24027661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档