2012-12-26 93 views
1

今天对个人应用程序有点小小的想法。从来没有尝试过Android,所以...任何帮助表示赞赏。使用Android进行计算

我想要的是2个输入字段,取这些值& *它们相互对接,在点击calc按钮后给出结果。

对我来说看起来还不错,但就像我说过的,我对Java并不熟悉,对Android也不熟悉。我的问题是,当我点击Calc按钮强制关闭/停止工作。提前致谢!

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 

    > 

    <EditText android:id="@+id/fuel" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/fuel" /> 

    <EditText android:id="@+id/numLaps" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/numLaps" /> 

    <Button 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/calc" 
     android:text="@string/calc" 
     android:onClick="calc"/> 

    <EditText 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/output" 
     android:inputType="number" 
     /> 



</LinearLayout> 

MainActivity.java

package com.example.iracingfuelcalc; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity implements android.view.View.OnClickListener { 


    Button calc; 
    EditText numLaps,fuel, output; 

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

     fuel = (EditText)findViewById(R.id.fuel); 
     numLaps = (EditText)findViewById(R.id.numLaps); 
     output = (EditText)findViewById(R.id.output); 
     calc = (Button)findViewById(R.id.calc); 


     calc.setOnClickListener(this); 

    } 
public void onClick(View arg0){ 

    int fuelValue = -1; 
    try { 
    fuelValue = Integer.parseInt(fuel.getText().toString()); 
    } 
    catch (NumberFormatException e) 
    { 
    //you don't have to do much here since fuelValue already has a default value (-1) 
    } 

    int lapsValue = -1; 
    try { 
     lapsValue = Integer.parseInt(numLaps.getText().toString()); 
    } 
    catch (NumberFormatException e) 
    { 

    } 

    int result = 0; 



     result = fuelValue * lapsValue; 

    result += Integer.parseInt(output.getText().toString()); 
    output.setText("" + result); 

} 

} 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">iRacing Fuel Calc</string> 
    <string name="fuel">Fuel per lap</string> 
    <string name="numLaps">Number of laps</string> 
    <string name="calc">Calculate</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_display_message">TEST</string> 
    <string name="output">0</string> 

</resources> 
+1

你在强制关闭时提供了堆栈跟踪吗?如果是的话,张贴它。 – mango

+1

你应该确保你的应用获得专利,然后再告诉世界它只是说。 – 2012-12-26 23:20:00

+0

Abe,其他应用程序都在那里,完全一样,我想要的。作为一名学生,在假期后学习更多关于Java的知识,我想提高自己的技能。谢谢,不过。 –

回答

4

你的第一个问题是你实际上没有给output一个值。在onCreate()

output = (EditText)findViewById(R.id.output); 

您还可能有因为任何整数解析是不是数字将抛出一个NumberFormatException问题。确保只输入数字或设置EditText以编程方式接受或验证。验证的一种方法是使用try/catch块。例如。

int fuelValue = -1; 
try { 
fuelValue = Integer.parseInt(fuel.getText().toString()); 
} 
catch (NumberFormatException e) 
{ 
//you don't have to do much here since fuelValue already has a default value (-1) 
} 

如果需要,可以通过android:inputType将EditText设置为仅接受数字。 docs有更多信息。

<EditText android:id="@+id/fuel" 
android:layout_weight="1" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:hint="@string/fuel" 

android:inputType = "number"/> 
+0

我试着理解并实施你说的话。我已经更新了上面的代码并添加了'strings.xml',以防出现问题。 错误仍然发生在同一个地方,谢谢。 –

+0

尝试将try/catch添加到'result + = Integer.parseInt(output.getText()。toString());'。如果这不起作用,您应该添加堆栈跟踪。 –

+0

它的工作原理,谢谢! –

0

二是:

Integer.parseInt(fuel.getText().toString()); 

您还没有验证字段的值。你可以在行动线程中得到NumberFormaException,这会导致应用程序崩溃