我正在开发一个无法正常工作的Android应用程序。 应用程序运行,但在最终计算中,无法识别每个级别的值。该程序没有向我指出任何错误。我无法理解可能出现的问题。
这是.Java文件
package com.com.calculartmb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
// variaveis
double altura;
double peso;
double resultado;
double nivel;
EditText pesoEd;
EditText alturaEd;
EditText finalEd;
SeekBar altSeekBar;
SeekBar pesoSeekBar;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
RadioButton radio4;
RadioButton radio5;
RadioGroup radioGrupo;
private double[] checklistValues = new double[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// iniciação
pesoEd = (EditText) findViewById(R.id.editText1);
alturaEd = (EditText) findViewById(R.id.editText2);
finalEd = (EditText) findViewById(R.id.editText3);
altSeekBar = (SeekBar) findViewById(R.id.seekBar1);
pesoSeekBar = (SeekBar) findViewById(R.id.seekBar2);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radio5 = (RadioButton) findViewById(R.id.radio5);
radioGrupo = (RadioGroup) findViewById(R.id.radioGroup1);
altSeekBar.setMax(25000);
pesoSeekBar.setMax(60000);
//listeners
addChangeListenerToRadios();
altSeekBar.setOnSeekBarChangeListener(altSeekBarListener);
pesoSeekBar.setOnSeekBarChangeListener(pesoSeekBarListener);
}
private OnSeekBarChangeListener pesoSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// Calcula o novo valor do TIP
peso = (pesoSeekBar.getProgress()) * .01;
// mostra na caixa o valor novo
pesoEd.setText(String.format("%.02f", peso).replace(',', '.'));
// Chama o update
updateValorTMB();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private OnSeekBarChangeListener altSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// Calcula o novo valor do TIP
altura = (altSeekBar.getProgress()) * .01;
// mostra na caixa o valor novo
alturaEd.setText(String.format("%.02f", altura).replace(',', '.'));
// Chama o update
updateValorTMB();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private void updateValorTMB() {
double peso = Double.parseDouble(pesoEd.getText().toString());
double altura = Double.parseDouble(alturaEd.getText().toString());
double resultado = 655 + (9.6 * peso) + (1.8 * altura) - (4.7 * nivel);
finalEd.setText(String.format("%.02f", resultado));
}
public void addChangeListenerToRadios(){
radioGrupo.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
checklistValues[0] = (radio1.isChecked())?1.2:0;
checklistValues[1] = (radio2.isChecked())?1.37:0;
checklistValues[2] = (radio3.isChecked())?1.65:0;
checklistValues[3] = (radio4.isChecked())?1.72:0;
checklistValues[4] = (radio5.isChecked())?1.9:0;
setNivelFromChecklist();
updateValorTMB();
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
}
});
}
private void setNivelFromChecklist(){
double total= 0;
for (double item:checklistValues){
total += item;
}
nivel = total * .01;
}
@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;
}
}
它给了我一个警告,即它没有被使用。
这是LogCat。因为应用程序无论如何都可以运行。
?:??: W/?(?): --------- beginning of /dev/log/main
03-10 15:50:37.119: I/Choreographer(914): Skipped 46 frames! The application may be doing too much work on its main thread.
03-10 15:56:17.109: D/dalvikvm(524): GC_FOR_ALLOC freed 453K, 14% free 3367K/3892K, paused 253ms, total 272ms
03-10 16:00:01.739: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 16:00:02.519: D/ConnectivityService(374): Done.
03-10 16:00:02.519: D/ConnectivityService(374): Setting timer for 720seconds
03-10 16:14:00.259: I/ProcessStatsService(374): Prepared write state in 139ms
03-10 16:15:00.139: I/ActivityManager(374): Killing 560:com.android.printspooler/u0a38 (adj 15): empty for 1846s
03-10 16:15:01.689: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 16:15:02.319: D/dalvikvm(374): GC_FOR_ALLOC freed 660K, 13% free 5786K/6648K, paused 525ms, total 528ms
03-10 16:15:02.479: D/ConnectivityService(374): Done.
03-10 16:15:02.489: D/ConnectivityService(374): Setting timer for 720seconds
03-10 16:15:02.969: D/MobileDataStateTracker(374): default: setPolicyDataEnable(enabled=true)
03-10 16:16:00.049: I/ActivityManager(374): Killing 991:com.android.keychain/1000 (adj 15): empty for 1814s
03-10 16:16:00.129: I/ActivityManager(374): Killing 451:android.process.media/u0a4 (adj 15): empty for 1815s
03-10 16:16:00.139: I/ActivityManager(374): Killing 834:com.android.music/u0a31 (adj 15): empty for 1834s
03-10 16:16:00.179: I/ActivityManager(374): Killing 620:com.android.defcontainer/u0a3 (adj 15): empty for 1836s
03-10 16:16:00.899: W/MediaFocusControl(374): RemoteControlClient died
03-10 16:17:48.239: D/dalvikvm(425): GC_FOR_ALLOC freed 1392K, 67% free 4302K/12768K, paused 331ms, total 333ms
03-10 16:18:10.359: D/dalvikvm(524): GC_FOR_ALLOC freed 563K, 17% free 3317K/3952K, paused 330ms, total 341ms
03-10 16:29:26.989: I/Choreographer(914): Skipped 105 frames! The application may be doing too much work on its main thread.
03-10 16:30:01.149: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 16:30:01.399: D/ConnectivityService(374): Done.
03-10 16:30:01.459: D/ConnectivityService(374): Setting timer for 720seconds
03-10 16:30:49.609: I/Choreographer(914): Skipped 51 frames! The application may be doing too much work on its main thread.
03-10 16:37:00.289: I/Choreographer(425): Skipped 31 frames! The application may be doing too much work on its main thread.
03-10 16:39:14.209: I/Choreographer(914): Skipped 38 frames! The application may be doing too much work on its main thread.
03-10 16:40:11.109: D/dalvikvm(524): GC_FOR_ALLOC freed 511K, 17% free 3318K/3952K, paused 376ms, total 376ms
03-10 16:40:12.779: I/Choreographer(914): Skipped 84 frames! The application may be doing too much work on its main thread.
03-10 16:40:34.109: I/Choreographer(914): Skipped 30 frames! The application may be doing too much work on its main thread.
03-10 16:41:49.109: I/Choreographer(914): Skipped 39 frames! The application may be doing too much work on its main thread.
03-10 16:44:00.599: I/ProcessStatsService(374): Prepared write state in 148ms
03-10 16:45:01.589: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 16:45:01.859: D/ConnectivityService(374): Done.
03-10 16:45:01.889: D/ConnectivityService(374): Setting timer for 720seconds
03-10 16:45:02.599: D/dalvikvm(374): GC_FOR_ALLOC freed 748K, 13% free 5789K/6648K, paused 408ms, total 409ms
03-10 16:45:02.659: E/NetdConnector(374): NDC Command {58 bandwidth gettetherstats} took too long (810ms)
03-10 16:45:02.799: D/MobileDataStateTracker(374): default: setPolicyDataEnable(enabled=true)
03-10 16:45:02.899: V/BackupManagerService(374): Running a backup pass
03-10 16:45:03.159: I/LatinIME:LogUtils(509): Dictionary info: dictionary = UserHistoryDictionary.en_US.dic ; version = ? ; date = 1394484303
03-10 16:45:03.169: V/BackupManagerService(374): clearing pending backups
03-10 16:45:03.239: V/PerformBackupTask(374): Beginning backup of 6 targets
03-10 16:45:03.239: I/PerformBackupTask(374): Initializing (wiping) backup state and transport storage
03-10 16:45:03.289: D/BackupManagerService(374): Now staging backup of android
03-10 16:45:03.319: D/BackupManagerService(374): Now staging backup of com.android.providers.settings
03-10 16:45:03.329: D/BackupManagerService(374): Now staging backup of com.android.providers.userdictionary
03-10 16:45:03.349: D/BackupManagerService(374): Now staging backup of com.android.sharedstoragebackup
03-10 16:45:03.369: D/BackupManagerService(374): Now staging backup of com.android.browser
03-10 16:45:03.399: D/BackupManagerService(374): Now staging backup of com.android.calendar
03-10 16:45:03.439: V/LocalTransport(374): wiping all data
03-10 16:45:03.559: D/PerformBackupTask(374): invokeAgentForBackup on @pm@
03-10 16:45:03.569: E/PerformBackupTask(374): Error invoking for backup on @pm@
03-10 16:45:03.619: I/PerformBackupTask(374): Backup pass finished.
03-10 16:45:03.629: E/PerformBackupTask(374): Duplicate finish
03-10 16:46:28.229: I/Choreographer(914): Skipped 39 frames! The application may be doing too much work on its main thread.
03-10 17:00:01.289: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 17:00:01.649: D/ConnectivityService(374): Done.
03-10 17:00:01.769: D/ConnectivityService(374): Setting timer for 720seconds
03-10 17:01:05.979: I/Choreographer(914): Skipped 71 frames! The application may be doing too much work on its main thread.
03-10 17:02:16.129: D/dalvikvm(524): GC_FOR_ALLOC freed 512K, 17% free 3317K/3952K, paused 293ms, total 297ms
03-10 17:06:00.589: D/dalvikvm(425): GC_FOR_ALLOC freed 538K, 67% free 4276K/12768K, paused 194ms, total 200ms
03-10 17:15:00.139: I/ProcessStatsService(374): Prepared write state in 53ms
03-10 17:15:01.669: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 17:15:02.319: D/dalvikvm(374): GC_FOR_ALLOC freed 750K, 13% free 5785K/6648K, paused 550ms, total 555ms
03-10 17:15:02.449: D/ConnectivityService(374): Done.
03-10 17:15:02.449: D/ConnectivityService(374): Setting timer for 720seconds
03-10 17:15:03.139: D/MobileDataStateTracker(374): default: setPolicyDataEnable(enabled=true)
03-10 17:15:39.759: I/Choreographer(914): Skipped 76 frames! The application may be doing too much work on its main thread.
03-10 17:20:46.539: I/Choreographer(914): Skipped 66 frames! The application may be doing too much work on its main thread.
03-10 17:24:09.389: D/dalvikvm(524): GC_FOR_ALLOC freed 512K, 17% free 3317K/3952K, paused 257ms, total 263ms
03-10 17:26:26.869: I/Choreographer(914): Skipped 36 frames! The application may be doing too much work on its main thread.
03-10 17:30:01.339: D/ConnectivityService(374): Sampling interval elapsed, updating statistics ..
03-10 17:30:01.489: D/ConnectivityService(374): Done.
03-10 17:30:01.489: D/ConnectivityService(374): Setting timer for 720seconds
: E/(): Device disconnected
答案 0 :(得分:2)
在你所拥有的new OnCheckedChangeListener
中,它包含了你应该使用的@Override
方法,而不是你自己的实现;
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
尝试将您的checkListValues
代码从您的方法移动到提供的代码。