日期:2014-05-20  浏览次数:20650 次

Android时间单位换算小程序运行时总是出现异常,但又找不出哪里错了,麻烦哪位高人指点一下。
这是一个转换时间单位的小程序,如果不加if判断语句的话,timeClearButton就可以正常使用,但如果加上一条判断语句,程序可以正常换算时间,但使用timeClearButton时程序就会被强行关闭,如果加两条判断语句,程序只能执行第一个判断语句要做的事情,若使用第二个判断,程序会被强行关闭!下面是程序代码:
Java code


package lygtc.ruanjian.liuxiuxiu02;

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

public class timeActivity extends Activity implements OnClickListener{
    
    private EditText     centuryText;        //世纪文本框
    private EditText     yearText;            //年文本框
    private EditText     monthText;            //月文本框
    private EditText     weekText;            //星期文本框
    private EditText     dayText;            //日文本框
    private EditText     hourText;            //小时文本框
    private EditText     minuteText;            //分钟文本框
    private EditText     secondText;            //秒文本框
    
    private Button         centuryButton;       //世纪按钮
    private Button         yearButton;           //年按钮
    private Button         monthButton;           //月按钮
    private Button         weekButton;           //星期按钮
    private Button         dayButton;           //日按钮
    private Button         hourButton;           //小时按钮
    private Button         minuteButton;       //分钟按钮
    private Button         secondButton;       //秒按钮
    
    private EditText     timeResultText;            //换算结果文本框
    
    private Button         timeClearButton;       //清空全部内容
    private Button         backButton;            //返回主菜单
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time);
        
        centuryText = (EditText)findViewById(R.id.centuryText);
        yearText = (EditText)findViewById(R.id.yearText);
        monthText = (EditText)findViewById(R.id.monthText);
        weekText = (EditText)findViewById(R.id.weekText);
        dayText = (EditText)findViewById(R.id.dayText);
        hourText = (EditText)findViewById(R.id.hourText);        
        minuteText = (EditText)findViewById(R.id.minuteText);
        secondText = (EditText)findViewById(R.id.secondText);
        timeResultText = (EditText)findViewById(R.id.timeResultText);
        
        centuryButton = (Button)findViewById(R.id.centuryButton);
        centuryButton.setOnClickListener(this);
        
        yearButton = (Button)findViewById(R.id.yearButton);
        yearButton.setOnClickListener(this);
        
        monthButton = (Button)findViewById(R.id.monthButton);
        monthButton.setOnClickListener(this);
        
        weekButton = (Button)findViewById(R.id.weekButton);
        weekButton.setOnClickListener(this);
        
        dayButton = (Button)findViewById(R.id.dayButton);
        dayButton.setOnClickListener(this);
        
        hourButton = (Button)findViewById(R.id.hourButton);
        hourButton.setOnClickListener(this);
        
        minuteButton = (Button)findViewById(R.id.minuteButton);
        minuteButton.setOnClickListener(this);
        
        secondButton = (Button)findViewById(R.id.secondButton);
        secondButton.setOnClickListener(this);
        
        backButton = (Button)findViewById(R.id.backButton);
        backButton.setOnClickListener(this);
        
        timeClearButton = (Button)findViewById(R.id.timeClearButton);
        timeClearButton.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();    
        
        switch(id){
            case R.id.backButton:{
                Intent backIntent = new Intent(this,Liuxiuxiu02Activity.class);
                startActivity(backIntent);
                break;
            }
            case R.id.timeClearButton:{
                centuryText.setText("");
                yearText.setText("");
                monthText.setText("");
                weekText.setText("");
                dayText.setText("");
                hourText.setText("");
                minuteText.setText("");
                secondText.setText("");
                timeResultText.setText("");
                break;
            }
        }
        
        if(centuryText.getText().toString() != null){            //第一个判断语句
            double  Century = Double.parseDouble(centuryText.getText().toString());
            yearText.setText("");
            monthText.setText("");
            weekText.setText("");
            dayText.setText("");
            hourText.setText("");
            minuteText.setText("");
            secondText.setText("");
            
            switch(id){
                case R.id.centuryButton:{                
                    double  century = Century*1;
                    timeResultText.setText(String.valueOf(century));
                    break;
                }
                case R.id.yearButton:{                
                    double  year = Century*100;
                    timeResultText.setText(String.valueOf(year));
                    break;
                }
                case R.id.monthButton:{
                    double  month = Century*100*12;
                    timeResultText.setText(String.valueOf(month));
                    break;
                }
                case R.id.weekButton:{
                    double  week = Century*100*12*52;
                    timeResultText.setText(String.valueOf(week));
                    break;
                }
                case R.id.dayButton:{
                    double  day = Century*100*12*52*7;
                    timeResultText.setText(String.valueOf(day));
                    break;
                }
                case R.id.hourButton:{
                    double  hour = Century*100*12*52*7*24;
                    timeResultText.setText(String.valueOf(hour));
                    break;
                }
                case R.id.minuteButton:{
                    double  minute = Century*100*12*52*7*24*60;
                    timeResultText.setText(String.valueOf(minute));
                    break;
                }
                case R.id.secondButton:{
                    double  second = Century*100*12*52*7*24*60*60;
                    timeResultText.setText(String.valueOf(second));
                    break;
                }
            
            }            
        }
        if(yearText.getText().toString() != null){            //第二个判断语句
            double  Year = Double.parseDouble(yearText.getText().toString());
            
            monthText.setText("");
            weekText.setText("");
            dayText.setText("");
            hourText.setText("");
            minuteText.setText("");
            secondText.setText("");
            
            switch(id){
                case R.id.centuryButton:{                
                    double  century = Year/100+Year%100;
                    timeResultText.setText(String.valueOf(century));
                    break;
                }
                case R.id.yearButton:{                
                    double  year = Year*1;
                    timeResultText.setText(String.valueOf(year));
                    break;
                }
                case R.id.monthButton:{
                    double  month = Year*12;
                    timeResultText.setText(String.valueOf(month));
                    break;
                }
                case R.id.weekButton:{
                    double  week = Year*12*52;
                    timeResultText.setText(String.valueOf(week));
                    break;
                }
                case R.id.dayButton:{
                    double  day = Year*12*52*7;
                    timeResultText.setText(String.valueOf(day));
                    break;
                }
                case R.id.hourButton:{
                    double  hour = Year*12*52*7*24;
                    timeResultText.setText(String.valueOf(hour));
                    break;
                }
                case R.id.minuteButton:{
                    double  minute = Year*12*52*7*24*60;
                    timeResultText.setText(String.valueOf(minute));
                    break;
                }
                case R.id.secondButton:{
                    double  second = Year*12*52*7*24*60*60;
                    timeResultText.setText(String.valueOf(second));
                    break;
                }
            
            }            
        }
        
        
    }
}