有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我正在构建一个兴趣计算应用程序,我的项目构建良好,有1个警告,但它不会在我的设备上打开

eclipse给我的警告是视图上的未命名导入警告。我在听,但我在用它。。。我很有信心这就是我的错误所在。请查看我的代码并更正所有错误。这是我第一次尝试从头开始编写应用程序。谢谢你抽出时间

这是我的主要任务,但它被称为sicalculator活动

package com.codeherenow.sicalculator;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.SeekBar;
import 安卓.widget.SeekBar.OnSeekBarChangeListener;
import 安卓.widget.TextView;

public class SICalculatorActivity extends Activity {

private Button calcButton;
private SeekBar yearsSeek = null;
private EditText principalAmount;
private EditText interestRate;
private TextView scaleReadout;
private TextView resultText;
float totalAmount;
float iRate;
float pAmount;
float years;
String result;

// public int progressChanged=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sicalculator);

    resultText = (TextView) findViewById(R.id.resultText);
    result=resultText.getText().toString();

    scaleReadout = (TextView) findViewById(R.id.scaleReadout);
    years=Float.parseFloat(scaleReadout.getText().toString());
    calcButton = (Button) findViewById(R.id.Calc_button);
    yearsSeek = (SeekBar) findViewById(R.id.seekBar1);

    principalAmount= (EditText) findViewById(R.id.editPrincipalAmount);
    pAmount=Float.parseFloat(principalAmount.getText().toString());
    interestRate = (EditText) findViewById(R.id.editIntrestRate);
    iRate = Float.parseFloat(interestRate.getText().toString());

    yearsSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        // int progressChanged = 0;

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            yearsSeek.setMax(50);
            scaleReadout.setText(progress + " Years");
        }

        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            // Toast.makeText(SeekBarActivity.this, progressChanged +
            // " Years", Toast.LENGTH_SHORT)
            // .show();
        }

    });
    // onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)

    calcButton.setOnClickListener(new OnClickListener() {
        public void onClick1(View v) {
            calculateinterest();
           finish();

        }



        private String calculateinterest() {
            // TODO Auto-generated method stub
             totalAmount=(pAmount*(iRate/100)*years);
            result=(totalAmount+"we did it!!");
             return result;
        }

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

        }
    });

  }

} 

这是我的主要观点。xml布局

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
  xmlns:tools="http://schemas.安卓.com/tools"
  安卓:layout_width="match_parent"
  安卓:layout_height="match_parent"
  安卓:paddingBottom="@dimen/activity_vertical_margin"
  安卓:paddingLeft="@dimen/activity_horizontal_margin"
  安卓:paddingRight="@dimen/activity_horizontal_margin"
  安卓:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".SICalculatorActivity" >

<TextView
    安卓:id="@+id/PrincipalAmount"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentLeft="true"
    安卓:layout_alignParentTop="true"
    安卓:layout_marginLeft="23dp"
    安卓:layout_marginTop="27dp"
    安卓:text="Principal Amount ($)"
    安卓:textAppearance="?安卓:attr/textAppearanceMedium" />

<EditText
    安卓:id="@+id/editPrincipalAmount"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/PrincipalAmount"
    安卓:layout_below="@+id/PrincipalAmount"
    安卓:ems="10"
    安卓:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<TextView
    安卓:id="@+id/InterestRate"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/editPrincipalAmount"
    安卓:layout_below="@+id/editPrincipalAmount"
    安卓:layout_marginTop="49dp"
    安卓:text="Interest Rate"
    安卓:textAppearance="?安卓:attr/textAppearanceMedium" />

<EditText
    安卓:id="@+id/editIntrestRate"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/InterestRate"
    安卓:layout_below="@+id/InterestRate"
    安卓:ems="10"
    安卓:inputType="numberDecimal" />

<SeekBar
    安卓:id="@+id/seekBar1"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentLeft="true"
    安卓:layout_below="@+id/editIntrestRate"
    安卓:layout_marginTop="55dp" />

<TextView
    安卓:id="@+id/scaleReadout"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignLeft="@+id/editIntrestRate"
    安卓:layout_below="@+id/editIntrestRate"
    安卓:layout_marginTop="32dp"
    安卓:text="0 Years"
    安卓:textAppearance="?安卓:attr/textAppearanceSmall" />

<TextView
    安卓:id="@+id/resultText"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_below="@+id/seekBar1"
    安卓:layout_centerHorizontal="true"
    安卓:layout_marginTop="31dp"
    安卓:text="@+string/result"
    安卓:textAppearance="?安卓:attr/textAppearanceMedium" />

<Button
    安卓:id="@+id/Calc_button"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentBottom="true"
    安卓:layout_centerHorizontal="true"
    安卓:layout_marginBottom="27dp"
    安卓:text="Calculate" />

</RelativeLayout>

我不确定这是否有用,但这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.codeherenow.sicalculator"
安卓:versionCode="1"
安卓:versionName="1.0" >

<uses-sdk
    安卓:minSdkVersion="19"
    安卓:targetSdkVersion="19" />

<application
    安卓:allowBackup="true"
    安卓:icon="@drawable/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name="com.codeherenow.sicalculator.SICalculatorActivity"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>

</manifest>

谢谢你花时间帮助贫穷无知的我!!非常感谢


共 (3) 个答案

  1. # 1 楼答案

    calcButton.setOnClickListener(new View.OnClickListener() {
    
            @Override//missing
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                calculateinterest();  // It is returning a String but you are not storing it anywhere. (Just Wondering)
           finish();  // I don't know why are you calling the finish() here. So I'm keeping it
            }
        });
    

    你的onClick监听器应该是这样的

    卡尔布顿。setOnClickListener(新视图.OnClickListener(){

        @Override//missing
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            result = calculateinterest();
            finish();
        }
    });
    

    然后像下面这样更改calculateinterest()

    private String calculateinterest() {
            // TODO Auto-generated method stub
    
             String amount = principalAmount.getText().toString();
             String rate = interestRate.getText().toString();
    
             if (amount.trim().equlas("") || rate.trim().equals(""))
              Toast.makeText(getApplicationContext(), "Please enter the values", Toast.Length_SHORT).show();
    
             else
             {
    
                 pAmount=Float.parseFloat(amount);
                 iRate = Float.parseFloat(rate);
    
    
    
                 totalAmount=(pAmount*(iRate/100)*years);
                 String output = "( " + totalAmount +" we did it!!) ";
                 return output;
    
             }
        }
    
  2. # 2 楼答案

    你的onclick听众错了。它缺少@override注释

    calcButton.setOnClickListener(new OnClickListener() {
        public void onClick1(View v) {
            calculateinterest();
           finish();
    
        }
    

    应该是这样的:

    calcButton.setOnClickListener(new View.OnClickListener() {
    
            @Override//missing
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                calculateinterest();
           finish();
            }
        });
    
  3. # 3 楼答案

    谢谢大家!你们都非常接近,但每个人似乎都没有注意到我没有设定搜索栏的进度,所以每次我的答案都是0.0!下面是运行良好的代码,它完全符合我的预期

    package com.codeherenow.sicalculator;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    public class SICalculatorActivity extends Activity {
    
    private Button calcButton;
    private SeekBar yearsSeek = null;
    private EditText principalAmount;
    private EditText interestRate;
    private TextView scaleReadout;
    private TextView resultText;
    float totalAmount;
    float iRate;
    float pAmount;
    float years;
    String output;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sicalculator);
    
        resultText = (TextView) findViewById(R.id.resultText);
    
    
        scaleReadout = (TextView) findViewById(R.id.scaleReadout);
    
        calcButton = (Button) findViewById(R.id.Calc_button);
        yearsSeek = (SeekBar) findViewById(R.id.seekBar1);
    
        principalAmount = (EditText) findViewById(R.id.editPrincipalAmount);
    
        interestRate = (EditText) findViewById(R.id.editIntrestRate);
    
    
        yearsSeek.setProgress(1);
        yearsSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
    
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                yearsSeek.setMax(50);
                scaleReadout.setText(progress + " Years");
                years=progress;
            }
    
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
    
        });
    
    
        calcButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                calculateinterest();
                resultText.setText(output);
    
                Toast.makeText(getApplicationContext(), ""+ totalAmount, Toast.LENGTH_SHORT)
                .show();
    
                        //scaleReadout.setText(progress + " Years");
               // setContentView(R.layout.sicalculator);
                //finish();
    
            }
        });}
    
      private void calculateinterest() {
        String amount = principalAmount.getText().toString();
        String rate = interestRate.getText().toString();
    
        if (amount.trim().equals("") || rate.trim().equals("")){
            Toast.makeText(getApplicationContext(), "Please enter the values", Toast.LENGTH_SHORT)
                    .show();
            }
        else{
            pAmount=Float.parseFloat(amount);
            iRate =Float.parseFloat(rate);
    
            totalAmount=(pAmount*(iRate/100)*years);
             output= "( "+ totalAmount+" we did it!!) ";
    
    
     }
    
     }}