Android应用程序功能问题

时间:2014-05-24 18:35:02

标签: android

我的程序功能无法正常运行。它没有做我需要它做的事情。我在代码中没有任何错误,但应用程序并没有按照我的意愿去做。

**更新:非常感谢你的帮助。该按钮显示TextView ce的值。但是,应用程序似乎没有运行我的if语句来获得CE1的新值,即= 4 * credit_1。应用程序采用CE1的初始化值= 0,并将其转换为字符串CE1s。因此在TextView中显示0.0。为什么应用程序不考虑我的ifstatement(grade_1.equals(" A"))?

这是MainActivity.java:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText course1 = (EditText)findViewById(R.id.Course1);
    EditText credit1 = (EditText)findViewById(R.id.editText7);
    final EditText grade1 = (EditText)findViewById(R.id.editText13);
    final Button b = (Button)findViewById(R.id.button1);
    final TextView ce = (TextView)findViewById(R.id.textView5);
    String grade_1 = grade1.getText().toString();
    String text = credit1.getText().toString();
    if (!TextUtils.isEmpty(text)) {
        // call parseDouble in here
        double credit_1 = Double.parseDouble(text); 
        double CE1=0;
        if (grade_1 =="A")
            CE1 = 4*credit_1;
        final String CE1s = Double.toString(CE1);

        b.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
            ce.setText(CE1s);
            }   
        });
    }

    }


@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }


    }
}

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rutgersgpacalculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.rutgersgpacalculator.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

1 个答案:

答案 0 :(得分:0)

确保验证文本是否为空并且单击它时它是否为rode。您还应该考虑在没有TextUtils的情况下进行验证,如if(text!= null&amp;&amp; text.length&gt; 0){...}

    b.setOnClickListener(new OnClickListener(){
    public void onClick(View v){

    // get the text when you click it to make sure it's live while click
    String text = credit1.getText().toString();

    if (!TextUtils.isEmpty(text)) {
    double credit_1 = Double.parseDouble(text); 
    ce.setText(Double.toString( (grade_1.equals("A")) ? 4*credit_1 : 0));
    }
 });