如何将Textview输出用于另一个类方法?

How to use Textview output for another class method?

提问人:AndraKenshin 提问时间:1/9/2023 最后编辑:greg-449AndraKenshin 更新时间:1/9/2023 访问量:44

问:

我刚刚开始使用 eclipse adt(android 开发者工具),我正在开发 electricbill 计算器应用程序。应用界面如下所示: 应用界面

用户将输入之前的消耗量和当前消耗量的值,然后计算(第一个按钮)以获得总消耗量。然后输入费率并计算(第二个按钮,乘以总消耗量和费率)以获得电费单。

我的主要活动包含按钮的 onClickListeners。计算的第一部分有效,但是当我尝试输入一个值并对其进行速率和计算时,应用程序崩溃了,我不知道问题出在哪里。这是我的代码:

“计算”按钮

public class MainActivity extends Activity {

    EditText PrevConEdt, CurrConEdt, RateEdt;
    TextView ConsumptionTv, ElectricBillTv;
    Button CalculateBtn, ComputeBtn, ClearBtn, ClearBtn2;
    Double rateDbl, cbillDbl, consumptionDbl, prevDbl, currentDbl;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        PrevConEdt = (EditText) findViewById(R.id.txtPreviousConsumption);
        CurrConEdt = (EditText) findViewById(R.id.txtCurrentConsumption);
        RateEdt = (EditText) findViewById(R.id.txtEnterRate);
        
        ConsumptionTv = (TextView) findViewById(R.id.txtConsumption);
        ElectricBillTv = (TextView) findViewById(R.id.txtEbill);
        
        CalculateBtn = (Button) findViewById(R.id.btnCalculate);
        CalculateBtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View Calculate) {
                
                String prev, current, consumption;
                 prev = PrevConEdt.getText().toString();
                 current = CurrConEdt.getText().toString();
                 
                 if(PrevConEdt.length() == 0){
                     Toast.makeText(MainActivity.this, "Consumption Required", Toast.LENGTH_LONG).show();
                     return;
                 }else if(CurrConEdt.length() == 0){
                     Toast.makeText(MainActivity.this, "Consumption Required", Toast.LENGTH_LONG).show();
                     return;
                     
                 }else{
                     Double prevDbl, currentDbl, consumptionDbl;
                     prevDbl = Double.parseDouble(prev);
                     currentDbl = Double.parseDouble(current);

                     
                     if(currentDbl < prevDbl){
                         //String msg = "Current should be greater than previous.";
                         //int duration = Toast.LENGTH_LONG;
                         //Toast.makeText(getBaseContext(), msg, duration).show();
                         //PrevConEdt.requestFocus();
                         CurrConEdt.setError("Current should be greater than previous.");
                         return;
                         
                         
                     }else{
                         consumptionDbl = currentDbl - prevDbl;
                         String consumptionStr = String.format("%.2f kWh", consumptionDbl);
                         ConsumptionTv.setText(consumptionStr);
                     }
                     
                     
                     
                     
                     
                 }
                 
                 
            } 
            
            
            
            
            
            
        });

计算按钮

ComputeBtn = (Button) findViewById(R.id.btnCompute);
        ComputeBtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View compute) {
                
                
                String rate, consumption;
                rate = RateEdt.getText().toString();
                consumption = ConsumptionTv.getText().toString();
                
                if(RateEdt.length() == 0){
                    RateEdt.setError("Rate is required.");
                    return;
                }else{
                    Double rateDbl, cbillDbl, consumptionDbl;
                    rateDbl = Double.parseDouble(rate);
                    consumptionDbl = Double.parseDouble(consumption);
                    
                    
                    cbillDbl = consumptionDbl *  rateDbl;
                    String billStr = String.format("Php %.2f", cbillDbl);
                    ElectricBillTv.setText(billStr);
                    
                    
                }
                
                
            }
        });
Android Eclipse ADT

评论

0赞 Atul Yadav 1/9/2023
共享崩溃日志。
0赞 AndraKenshin 1/9/2023
嗨,我不确定这是否!条目 org.eclipse.jdt.ui 4 10001 2023-01-09 22:58:20.767 !消息内部错误!堆栈 1 Java 模型异常:Java 模型状态 [TextView 的未知 javadoc 格式 [在 TextView.class 中 [在 android.widget 中 [在 C:\Users\TOSHIBA SAT-C655 PC\Downloads\MOBILE APP\EclipseADT\sdk\platforms\android-17\android.jar]]]] !子条目 1 org.eclipse.jdt.core 4 1009 2023-01-09 22:58:20.767 !消息:TextView 的未知 javadoc 格式 [在 TextView 中.class [在 android.widget 中 [在 C:\Users\TOSHIBA SAT-C655 PC\Downloads\MOBILE APP\EclipseADT\sdk\platforms\android-17\android.jar]]]

答: 暂无答案