我想计算工作时间和赚取的现金(假设每小时20 ILS - 这就是为什么我在我的申请中乘以20的原因。)
我使用SharedPreferences
所以我可以从应用程序中退出,它仍然会记得当我按下开始按钮时,我也在使用Calendar.getInstance()
但是当我得到我正在使用的时间时小时格式,所以我不会有计算工作时间的问题(如果我在早上8点开始并且在晚上9点结束,工作时间将完成减去开始,9-8和那个小时,所以这将是错误的)。
我在开始时使用了INTs
,但由于你无法在INTs
工作0.3小时,因此我将工作时间缩短为零,我会将其切换为零,但我更改为Doubles
但令人惊讶的是getDouble
中没有putDouble
或SharedPreferences
,因此我切换到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 android.app.SharedPreferencesImpl.getFloat(SharedPreferencesImpl.java:253)
at firstappdevelopment.work.MainActivity$2.onClick(MainActivity.java:57)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
我很抱歉,如果它是半个logcat,我用了搜索" MainActivity"因为在Android Studio Logcat中有自动滚动,我无法进行崩溃和复制。第MainActivity.java:57 refers to:
行
cash = prefs.getFloat("key", 0);
根据我的理解,它说现金是int
,但看看我的构造函数(?):
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 android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.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:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total:"
android:layout_alignTop="@+id/stop"
android:layout_toRightOf="@+id/stop"
android:layout_toEndOf="@+id/stop"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:layout_alignTop="@+id/start"
android:layout_toRightOf="@+id/start"
android:layout_toEndOf="@+id/start" />
</RelativeLayout>
答案 0 :(得分:1)
因此,从您收到的错误ClassCastException
开始,如果该值不存在,则您尝试从0
传递默认值SharedPreferences
。
将cash = prefs.getFloat("key", 0)
更改为cash = prefs.getFloat("key", 0.0)
答案 1 :(得分:1)
因为你有这个例外:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float
你的问题在这里:
cash = prefs.getFloat("key", 0);
必须是:
cash = prefs.getFloat("key", 0.0f);
cash
可能定义为int!你必须改为:float cash;
有关getFloat()方法的更多信息。
答案 2 :(得分:-1)
这种情况正在发生,因为在prefs = getSharedPreferences("myPrefsKey", 0);
0中是一个int。使用0.0将解决问题,因为它是一个浮点数。更正代码 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("myPrefsKey", 0.0); // CODE CHANGED HERE
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);
}
});
}
答案 3 :(得分:-1)
所以你知道问题在这里:
cash = prefs.getFloat("key", 0)
我认为您的代码很好!
但是,正如您所说,您最初在“key”的共享首选项中存储了一些整数值。你删除了这些值吗?
我建议您尝试将cash = prefs.getFloat("key", 0)
更改为cash = prefs.getFloat("key_new", 0)
。这可能是你忽略的愚蠢问题!
注意,如果cash = prefs.getFloat("key_new", 0)
中存在0或0f或0.0f的问题,则会出现编译时错误。