我可能会遗漏一些基本的东西,但我还是初学者。我查了很多网站,但我似乎无法使这些修复工作。我有一个偏好片段,用户可以输入他们的小时工资(以及其他内容)。我有一个活动,应该在计算中使用该偏好,但我不断得到0返回。这是代码:
public class StartCalc extends Activity implements OnSharedPreferenceChangeListener {
static final String TAG = "StartCalc";
Date time_o, time_f;
float money_o = 0, money_f = 0, hoursToday = 0, hoursTotal = 0, payscale;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
payscale = prefs.getFloat("PAYSCALE", 0);
}
public float CalculateWeekly() {
Log.d(TAG, "Calculating normaltime pay");
float pay = 10 * payscale; // testing with 10 instead of hours
// total
Log.d(TAG, Double.toString(payscale)); //always getting 0.0
return pay;
}
在我的开始活动中,我有一个切换按钮,该按钮应该通过比较您的工作时间与首选项中设置的工资水平来计算工资。
@Override
public void onClick(View v) {
boolean on = ((ToggleButton) v).isChecked();
StartCalc session = new StartCalc();
Intent timeServiceIntent = new Intent(this, TimeService.class);
if (on == true) {
button = true;
Log.d(TAG, "Button Clicked");
// session.TimeClock();
startService(timeServiceIntent);
} else {
button = false;
// Log.d(TAG, "Not Running");
// session.CalculateWeekly();
try {
tvTotal.setText(Float.toString(session.CalculateWeekly()));
} catch (Exception e) {
Log.d(TAG, "Set text didn't work...");
e.printStackTrace();
}
Toast.makeText(Home.this, "Calculating...", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
嗯...
当你调用new StartCalc()它不会调用onCreateMethod并且payscale为0时,你无法真正实例化这样的活动。
我甚至没有看到你的StartCalc扩展Activity的一点,因为你没有设置contentView,是不是要获取上下文?您可以通过其他方式获取上下文,请尝试以下方式:
public class StartCalc implements OnSharedPreferenceChangeListener {
static final String TAG = "StartCalc";
Date time_o, time_f;
float money_o = 0, money_f = 0, hoursToday = 0, hoursTotal = 0, payscale;
SharedPreferences prefs;
private Context context;
public StartCalc(Context context) {
this.context = context;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(this);
payscale = prefs.getFloat("PAYSCALE", 0);
}
public float calculateWeekly() { //its good practice to start method names with lower case
Log.d(TAG, "Calculating normaltime pay");
float pay = 10 * payscale; // testing with 10 instead of hours
// total
Log.d(TAG, Double.toString(payscale)); //always getting 0.0
return pay;
}
然后在你的onClick方法上试试这个:
@Override
public void onClick(View v) {
boolean on = ((ToggleButton) v).isChecked();
StartCalc session = new StartCalc(MainActivity.this); //im assuming its called main activity
Intent timeServiceIntent = new Intent(this, TimeService.class);
if (on == true) {
button = true;
Log.d(TAG, "Button Clicked");
// session.TimeClock();
startService(timeServiceIntent);
} else {
button = false;
// Log.d(TAG, "Not Running");
// session.calculateWeekly();
try {
tvTotal.setText(Float.toString(session.CalculateWeekly()));
} catch (Exception e) {
Log.d(TAG, "Set text didn't work...");
e.printStackTrace();
}
Toast.makeText(Home.this, "Calculating...", Toast.LENGTH_SHORT).show();
}
}