Android的应用程序的折扣没有运行 [英] Android discount app is not running

查看:108
本文介绍了Android的应用程序的折扣没有运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Android简单的打折应用程序,但我的应用程序没有运行,希望你们能帮助我弄清楚什么不妥的地方。
我是新android开发,所以也许我做了很多事情错了,这里是我的code。

现在我才刚刚工作的单选按钮的一部分,在那里我按一下按钮,让我们说10%,那么它会显示正确保存量和总

 进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.view.View;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.widget *。

公共类MainActivity延伸活动{

私人双人listedPrice;
私人诠释折扣precentID;
私人RadioGroup中折扣;
私人双重优惠precent;
//私人单选折扣precent;
私人TextView的savedAmount;
私人TextView的payAmount;
私人的EditText userInput;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    userInput =(EditText上)findViewById(R.id.editText1);
    listedPrice = Double.parseDouble(userInput.getText()的toString());
    savedAmount =(TextView中)findViewById(R.id.textView7);
    payAmount =(TextView中)findViewById(R.id.textView8);
    折扣=(RadioGroup中)findViewById(R.id.radioGroup1);
    //搜索栏搜索栏=(搜索栏)findViewById(R.id.seekBar1);
    // seekBar.setOnSeekBarChangeListener(seekBarListener);
}

公共无效的onClick(视图v){

    折扣precentID = discount.getCheckedRadioButtonId();
    //折扣precent =(单选)findViewById(优惠precentID);
    开关(优惠precentID){
    案例R.id.radio0:{
        折扣precent = 0.10;
        savedAmount.setText($+ listedPrice *折扣precent);
        payAmount.setText($
                +(listedPrice  - (listedPrice *折扣precent)));
        打破;
    }
    案例R.id.radio1:{
        折扣precent = 0.25;
        savedAmount.setText($+ listedPrice *折扣precent);
        payAmount.setText($
                +(listedPrice  - (listedPrice *折扣precent)));
        打破;
    }
    案例R.id.radio2:{
        折扣precent = 0.50;
        savedAmount.setText($+ listedPrice *折扣precent);
        payAmount.setText($
                +(listedPrice  - (listedPrice *折扣precent)));
        打破;
    }
    }

}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}

}
 

解决方案

这是最有可能您的问题

  listedPrice = Double.parseDouble(userInput.getText()的toString());
 

用户一直没有在的onCreate进入一些与此code()的机会所以,除非你有一些硬codeD,你将获得 NumberFormatException异常。你需要把那个code在的onClick()或者用户后,其他一些事件已进入东西的机会。

您也应该做一些错误检查,如把一个的try / catch它里面

 尝试
{
    listedPrice = Double.parseDouble(userInput.getText()的toString());
}
赶上(NumberFormatException的E)
{
    //做一些事情,如果无效的双
}
 

在这里发帖,你需要清楚地说明什么是/不工作的期望和你的问题。另外,如果你的应用程序崩溃,那么就会出现在logcat的输出,你需要发布的,所以我们可以很容易地看到什么/在哪里出了问题。

Eclipse的不说,你有任何问题,因为这是一个运行时错误,这意味着Eclipse中没有看到什么错在编译时。这意味着你的语法是正确的,尽可能的Eclipse知道,但你的逻辑,是不是。

I am making a simple discount app for android, but my app is not running and hope you guys can help me to figure whats wrong with it.
I am new to android development, so maybe I am doing a lot of things wrong and here's my code.

Right now I am just just working on the RadioButton part, where I click the Button, let's say 10%, then it will display the correctly saved amount and the total

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.*;

public class MainActivity extends Activity {

private double listedPrice;
private int discountPrecentID;
private RadioGroup discount;
private double discountPrecent;
// private RadioButton discountPrecent;
private TextView savedAmount;
private TextView payAmount;
private EditText userInput;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    userInput = (EditText) findViewById(R.id.editText1);
    listedPrice = Double.parseDouble(userInput.getText().toString());
    savedAmount = (TextView) findViewById(R.id.textView7);
    payAmount = (TextView) findViewById(R.id.textView8);
    discount = (RadioGroup) findViewById(R.id.radioGroup1);
    // SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
    // seekBar.setOnSeekBarChangeListener(seekBarListener);
}

public void onClick(View v) {

    discountPrecentID = discount.getCheckedRadioButtonId();
    // discountPrecent = (RadioButton) findViewById(discountPrecentID);
    switch (discountPrecentID) {
    case R.id.radio0:{
        discountPrecent = 0.10;
        savedAmount.setText("$" + listedPrice * discountPrecent);
        payAmount.setText("$"
                + (listedPrice - (listedPrice * discountPrecent)));
        break;
    }
    case R.id.radio1:{
        discountPrecent = 0.25;
        savedAmount.setText("$" + listedPrice * discountPrecent);
        payAmount.setText("$"
                + (listedPrice - (listedPrice * discountPrecent)));
        break;
    }
    case R.id.radio2:{
        discountPrecent = 0.50;
        savedAmount.setText("$" + listedPrice * discountPrecent);
        payAmount.setText("$"
                + (listedPrice - (listedPrice * discountPrecent)));
        break;
    }
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

解决方案

This is most likely your problem

listedPrice = Double.parseDouble(userInput.getText().toString());

The user hasn't had a chance to enter something with this code in onCreate() so unless you have something hard-coded, you will get a NumberFormatException. You need to place that code in an onClick() or some other event after the user has had a chance to enter something.

You should also do some error-checking such as putting it inside of a try/catch.

try
{
    listedPrice = Double.parseDouble(userInput.getText().toString());
}
catch (NumberFormatException e)
{
    // do something if invalid double
}

When posting here, you need to clearly state what is/isn't working as expected and what your problem is. Also, if your app crashes then there will be output in the logcat and you need to post that so we can easily see what/where the problem is.

Eclipse didn't say you have any problem because this is a runtime error which means that Eclipse didn't see anything wrong at compile time. Meaning your syntax is correct, as far as Eclipse knows, but your logic is not.

这篇关于Android的应用程序的折扣没有运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆