这是程序,它可以工作,但是当我在输入框中没有数字时拉搜索栏时,程序退出并且警报对话框没有完成它的工作,即使我尝试并抓住代码。
public class MainActivity extends Activity {
private SeekBar sbCash;
private Button btnten,btntwenty,btnthirty;
private TextView tvShwProg,tvfinal;
private EditText etCash;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCash = (EditText)findViewById(R.id.etCash);
sbCash = (SeekBar)findViewById(R.id.sbCash);
tvShwProg = (TextView)findViewById(R.id.tvShwProg);
tvfinal = (TextView)findViewById(R.id.tvFinal);
btnten = (Button)findViewById(R.id.btnten);
btntwenty = (Button)findViewById(R.id.btntwenty);
btnthirty = (Button)findViewById(R.id.btnthirty);
etCash.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable arg0)
{
}
public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3)
{
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
try{
guiChanged();
}
catch(Exception e)
{
e.printStackTrace();
errormessage();
}
}
});
SeekBar.OnSeekBarChangeListener sbListener = new SeekBar.OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar) {}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
try{
guiChanged();
}
catch(Exception e)
{
e.printStackTrace();
errormessage();
}
}
};
sbCash.setOnSeekBarChangeListener(sbListener);
dosomethig();
dosomethig2();
dosomething3();
}
public AlertDialog errormessage()
{
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Fire Missile?")
.setPositiveButton("Exit!", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Fix", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public void guiChanged()
{
try{
DecimalFormat fmt = new DecimalFormat ("0.00");
int CashMoney = sbCash.getProgress();
tvShwProg.setText("Current Percentage " +CashMoney+ "%");
double amount = Double.parseDouble(etCash.getText().toString());
double moneydue = (amount * (CashMoney/100.0));
double totalamount = (moneydue + amount);
tvfinal.setText("Tip " + fmt.format(moneydue)+ " Total "+ fmt.format(totalamount));
}
catch(Exception e)
{
errormessage();
e.printStackTrace();
etCash.setText("Bro enter a number here");
}
}
public void dosomethig()
{
btnten.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sbCash.setProgress(10);
}
});
}
public void dosomethig2()
{
btntwenty.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sbCash.setProgress(15);
}
});
}
public void dosomething3()
{
btnthirty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sbCash.setProgress(20);
}
});
}
日志猫:
05-12 19:44:27.144: E/ActivityManager(71): ANR in com.Shashank.nchs.Final_exam.hayes (com.Shashank.nchs.Final_exam.hayes/.MainActivity)
05-12 19:44:27.144: E/ActivityManager(71): Reason: keyDispatchingTimedOut
05-12 19:44:27.144: E/ActivityManager(71): Load: 0.57 / 0.57 / 0.27
05-12 19:44:27.144: E/ActivityManager(71): CPU usage from 21865ms to -1ms ago:
05-12 19:44:27.144: E/ActivityManager(71): 12% 431/com.Shashank.nchs.Final_exam.hayes: 10% user + 1.9% kernel / faults: 1929 minor 1 major
05-12 19:44:27.144: E/ActivityManager(71): 0.4% 176/logcat: 0.2% user + 0.2% kernel
05-12 19:44:27.144: E/ActivityManager(71): 1.3% 41/adbd: 0.1% user + 1.1% kernel
05-12 19:44:27.144: E/ActivityManager(71): 1.3% 71/system_server: 1% user + 0.2% kernel / faults: 102 minor
05-12 19:44:27.144: E/ActivityManager(71): 0% 124/jp.co.omronsoft.openwnn: 0% user + 0% kernel / faults: 22 minor
05-12 19:44:27.144: E/ActivityManager(71): 0.4% 141/com.android.launcher: 0.2% user + 0.1% kernel / faults: 50 minor
05-12 19:44:27.144: E/ActivityManager(71): 0.3% 199/android.process.acore: 0.3% user + 0% kernel / faults: 39 minor
05-12 19:44:27.144: E/ActivityManager(71): 0.2% 255/com.android.quicksearchbox: 0.1% user + 0% kernel / faults: 315 minor
05-12 19:44:27.144: E/ActivityManager(71): 0.2% 267/com.android.defcontainer: 0.2% user + 0% kernel / faults: 37 minor
05-12 19:44:27.144: E/ActivityManager(71): 0% 31/surfaceflinger: 0% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 0% 115/com.android.systemui: 0% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 0% 136/com.android.phone: 0% user + 0% kernel / faults: 3 minor
05-12 19:44:27.144: E/ActivityManager(71): 34% TOTAL: 24% user + 9.1% kernel + 0.5% softirq
05-12 19:44:27.144: E/ActivityManager(71): CPU usage from 1008ms to 1562ms later:
05-12 19:44:27.144: E/ActivityManager(71): 53% 431/com.Shashank.nchs.Final_exam.hayes: 48% user + 5% kernel / faults: 265 minor
05-12 19:44:27.144: E/ActivityManager(71): 32% 431/inal_exam.hayes: 30% user + 2.5% kernel
05-12 19:44:27.144: E/ActivityManager(71): 8.7% 434/GC: 8.7% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 2.5% 437/Compiler: 2.5% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 1.2% 433/HeapWorker: 1.2% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 5.4% 71/system_server: 3.6% user + 1.8% kernel / faults: 5 minor
05-12 19:44:27.144: E/ActivityManager(71): 5.4% 95/InputDispatcher: 1.8% user + 3.6% kernel
05-12 19:44:27.144: E/ActivityManager(71): 1.8% 76/Compiler: 1.8% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 5.4% 176/logcat: 3.6% user + 1.8% kernel
05-12 19:44:27.144: E/ActivityManager(71): 3.7% 371/com.svox.pico: 3.7% user + 0% kernel / faults: 26 minor
05-12 19:44:27.144: E/ActivityManager(71): 2.4% 371/com.svox.pico: 2.4% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 3.6% 41/adbd: 1.8% user + 1.8% kernel
05-12 19:44:27.144: E/ActivityManager(71): 7.2% 41/adbd: 3.6% user + 3.6% kernel
05-12 19:44:27.144: E/ActivityManager(71): 3.6% 161/adbd: 1.8% user + 1.8% kernel
05-12 19:44:27.144: E/ActivityManager(71): 1.8% 162/adbd: 0% user + 1.8% kernel
05-12 19:44:27.144: E/ActivityManager(71): 1.1% 267/com.android.defcontainer: 1.1% user + 0% kernel
05-12 19:44:27.144: E/ActivityManager(71): 100% TOTAL: 85% user + 14% kernel