java计算工作时间和现金
我想计算工作时间和挣到的现金(假设每小时20 ILS——这就是为什么我在应用程序中乘以20。)
我使用SharedPreferences
这样我就可以从应用程序中出来,它仍然记得我按下开始按钮的时候,我也在使用Calendar.getInstance()
但是当我得到小时数时,我使用的是12小时格式,这样我就不会有计算工作时间的问题(如果我在上午8点开始,晚上9点结束,工作时间将是完成减去开始,9-8,这是一个小时,所以它是错误的)
一开始我用了INTs
,但我一直在零小时工作,因为你不能在INTs
中工作0.3小时,它会将它减少到零,我改为Doubles
,但令人惊讶的是,SharedPreferences
中没有getDouble
或^Floats
,但我现在出现了错误,我的应用程序崩溃了
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("myPrefsKey", 0);
textView = (TextView) findViewById(R.id.textView);
buttonStart = (Button) findViewById(R.id.start);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "onClick: starting");
c = Calendar.getInstance();
begmin = c.get(Calendar.MINUTE);
beghour = c.get(Calendar.HOUR_OF_DAY);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("key1", begmin);
edit.putInt("key2", beghour);
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "onClick: stopping");
Calendar c = Calendar.getInstance();
finmin = c.get(Calendar.MINUTE);
finhour = c.get(Calendar.HOUR_OF_DAY);
if (finhour >= beghour)
workhour = finhour - beghour;
else
workhour = 12 - beghour + finhour;
workmin = finmin - begmin;
hourcashto = (float)(workhour * 20);
mincashto = (float)(20 * (workmin / 60));
cashto = hourcashto + mincashto;
cash = prefs.getFloat("key", 0);
cash = cash + cashto;
SharedPreferences.Editor edit = prefs.edit();
edit.putFloat("key", cash);
edit.commit();
textView.setText("cash now: " + cashto + " cash total including today: " + cash + " \n" + "Time today: " + workhour + ":" + workmin);
}
});
}
以下是Logcat输出:
09-01 19:44:19.811 17344-17344/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: firstappdevelopment.work, PID: 17344
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float
at 安卓.app.SharedPreferencesImpl.getFloat(SharedPreferencesImpl.java:253)
at firstappdevelopment.work.MainActivity$2.onClick(MainActivity.java:57)
at 安卓.view.View.performClick(View.java:5254)
at 安卓.view.View$PerformClick.run(View.java:21179)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:145)
at 安卓.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
at de.robv.安卓.xposed.XposedBridge.main(XposedBridge.java:115)
很抱歉,如果是半个logcat,我使用了搜索“MainActivity”,因为Android Studio logcat中有自动滚动,我无法访问崩溃和复制。行MainActivity.java:57 refers to:
cash = prefs.getFloat("key", 0);
据我所知,它说现金是一种^{
private static final String TAG = "ServicesDemo";
private Button buttonStart, buttonStop;
private int begmin, beghour, finmin, finhour, workhour, workmin;
private float cash, cashto, hourcashto, mincashto;
private Calendar c;
private TextView textView;
private SharedPreferences prefs;
谢谢大家的帮助
编辑,完整代码+XML
package firstappdevelopment.work;
import 安卓.app.Activity;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import java.util.Calendar;
public class MainActivity extends Activity {
private static final String TAG = "ServicesDemo";
private Button buttonStart, buttonStop;
private int begmin, beghour, finmin, finhour, workhour, workmin;
private float cash, cashto, hourcashto, mincashto;
private Calendar c;
private TextView textView;
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("myPrefsKey", 0);
textView = (TextView) findViewById(R.id.textView);
buttonStart = (Button) findViewById(R.id.start);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "onClick: starting");
c = Calendar.getInstance();
begmin = c.get(Calendar.MINUTE);
beghour = c.get(Calendar.HOUR_OF_DAY);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("key1", begmin);
edit.putInt("key2", beghour);
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "onClick: stopping");
Calendar c = Calendar.getInstance();
finmin = c.get(Calendar.MINUTE);
finhour = c.get(Calendar.HOUR_OF_DAY);
if (finhour >= beghour)
workhour = finhour - beghour;
else
workhour = 12 - beghour + finhour;
workmin = finmin - begmin;
hourcashto = (float)(workhour * 20);
mincashto = (float)(20 * (workmin / 60));
cashto = hourcashto + mincashto;
cash = prefs.getFloat("key", 0f);
cash = cash + cashto;
SharedPreferences.Editor edit = prefs.edit();
edit.putFloat("key", cash);
edit.commit();
textView.setText("cash now: " + cashto + " cash total including today: " + cash + " \n" + "Time today: " + workhour + ":" + workmin);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML:
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
安卓:id="@+id/start"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="START"
安卓:layout_alignParentTop="true"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true" />
<TextView
安卓:id="@+id/textView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Total:"
安卓:layout_alignTop="@+id/stop"
安卓:layout_toRightOf="@+id/stop"
安卓:layout_toEndOf="@+id/stop"
/>
<Button
安卓:id="@+id/stop"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="STOP"
安卓:layout_alignTop="@+id/start"
安卓:layout_toRightOf="@+id/start"
安卓:layout_toEndOf="@+id/start" />
</RelativeLayout>
# 1 楼答案
既然你有这个例外:
你的问题是:
必须是:
或者您的变量
cash
可能被定义为int!您必须切换到:更多关于getFloat()方法的信息
# 2 楼答案
因此,从收到的错误
ClassCastException
中,如果值不存在,则尝试从SharedPreferences
传递一个默认值0
将
cash = prefs.getFloat("key", 0)
更改为cash = prefs.getFloat("key", 0.0)
# 3 楼答案
所以你知道问题就在这里:
我认为你的代码很好! 但是,正如您所说,您最初是在“key”的共享首选项中存储一些整数值。你删除了那些值吗? 我建议你试着把
cash = prefs.getFloat("key", 0)
改成cash = prefs.getFloat("key_new", 0)
。这可能是你忽略的愚蠢问题注意,如果在
cash = prefs.getFloat("key_new", 0)
中存在0或0f或0.0f的问题,那么它会给您一个编译时错误