问题描述
计算公式:总长途旅行(当前里程表 - previous里程表)/无。升汽油泵
我已经做了我的code这个公式,但是我的code并没有完全完成。
我的code是(当前里程表/没有。公升汽油)。
我想减远previous里程表,但我不知道该怎么做。
我将存储我的数据库中的信息的基础上的日期。
有人可以指导我怎么可以这样做?
为了实现总距离旅行(当前里程表 - previous里程表)的公式/没有。升汽油泵..
这是我的code:
我知道有什么毛病我的字符串lastOdm =; ,布提不知道该怎么解决这个错误。
logcat的:
11月1日至22日:01:12.136:E / AndroidRuntime(7078):致命异常:主要
11月1日至22日:01:12.136:E / AndroidRuntime(7078):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.fuellog / com.example.fuellog.MainActivity}:显示java.lang.NullPointerException
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.os.Handler.dispatchMessage(Handler.java:99)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.os.Looper.loop(Looper.java:137)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.ActivityThread.main(ActivityThread.java:5041)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在java.lang.reflect.Method.invokeNative(本机方法)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在java.lang.reflect.Method.invoke(Method.java:511)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在dalvik.system.NativeStart.main(本机方法)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):因:显示java.lang.NullPointerException
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在com.example.fuellog.DBAdapter.getLastOdometer(DBAdapter.java:104)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在com.example.fuellog.MainActivity.onCreate(MainActivity.java:74)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.Activity.performCreate(Activity.java:5104)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11月1日至22日:01:12.136:E / AndroidRuntime(7078):11 ...更多
mainactivity.java
公共类MainActivity延伸活动{
// TableLayout tablelayout_Log = NULL;
按钮saveButton = NULL;
按钮cancelButton = NULL;
//按钮搜寻] = NULL;
静态的EditText dateEdit;
的EditText priceEdit;
的EditText pumpEdit;
TextView的costView;
的EditText odometerEdit;
TextView的fconView;
TextWatcher textWatcher;
串priceEditStr =,pumpEditStr =;
串odmEditStr =;
串lastOdm =;双重效果;
双resultCon;
私人诠释mYear;
私人诠释mMonth;
私人诠释MDAY;静态最终诠释DATE_DIALOG_ID = 0; 公共布尔ISNUMERIC(字符串str)
{
返回str.matches( - \\\\ D +(\\\\ D +)??);
} @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
costView =(的TextView)findViewById(R.id.tcost);
dateEdit =(EditText上)findViewById(R.id.date);
priceEdit =(EditText上)findViewById(R.id.fuelprice);
pumpEdit =(EditText上)findViewById(R.id.fuelpump);
odometerEdit =(EditText上)findViewById(R.id.odometer);
fconView =(的TextView)findViewById(R.id.fcon); dateEdit.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//的ShowDialog(DATE_DIALOG_ID);
DialogFragment newFragment =新DatePickerFragment();
newFragment.show(getFragmentManager(),日期选择器);
}
});
priceEdit.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效afterTextChanged(编辑编辑){
//这里,当我们在引进的EditText的东西,我们从它的字符串
如果(!。priceEdit.getText()的toString()修剪()equalsIgnoreCase()及&安培;!。priceEdit.getText()的toString()修剪()equalsIgnoreCase(NULL))
priceEditStr = priceEdit.getText()的toString()修剪()。
如果(!。pumpEdit.getText()的toString()修剪()equalsIgnoreCase()及&安培;!。pumpEdit.getText()的toString()修剪()equalsIgnoreCase(NULL))
pumpEditStr = pumpEdit.getText()的toString()修剪()。 如果(!。priceEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器;!pumpEdit.getText()的toString()修剪()equalsIgnoreCase(。))
{
结果= Double.parseDouble(priceEditStr)* Double.parseDouble(pumpEditStr);
costView.setText(+结果);
} }
}); pumpEdit.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效afterTextChanged(编辑编辑){
//这里,当我们在引进的EditText的东西,我们从它的字符串
如果(!priceEdit.getText()。的toString()。修剪()。equalsIgnoreCase())
priceEditStr = priceEdit.getText()的toString()修剪()。
如果(!pumpEdit.getText()。的toString()。修剪()。equalsIgnoreCase())
pumpEditStr = pumpEdit.getText()的toString()修剪()。
如果(!。priceEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器;!pumpEdit.getText()的toString()修剪()equalsIgnoreCase(。))
{
结果= Double.parseDouble(priceEditStr)* Double.parseDouble(pumpEditStr);
costView.setText(+结果);
} }
}); odometerEdit.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效afterTextChanged(编辑编辑){
//这里,当我们在引进的EditText的东西,我们从它的字符串 如果(!odometerEdit.getText()。的toString()。修剪()。equalsIgnoreCase())
odmEditStr = odometerEdit.getText()的toString()修剪()。
!。如果(odometerEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器;!pumpEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器; !lastOdm.trim()equalsIgnoreCase(空)及&放大器;!lastOdm.trim()equalsIgnoreCase())
{
DBAdapter dbAdaptor =新DBAdapter(getApplicationContext());
lastOdm = dbAdaptor.getLastOdometer();
resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(lastOdm)/ Double.parseDouble(pumpEditStr);
fconView.setText(+ resultCon);
} }
}); cancelButton =(按钮)findViewById(R.id.cancelBTN);
cancelButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根 ///有关创建意向,并开始活动
意图summaryView =新意图(getApplicationContext(),summary.class);
startActivity(summaryView);
}
}); //插
saveButton =(按钮)findViewById(R.id.saveBTN);
saveButton.setOnClickListener(新OnClickListener()
{
公共无效的onClick(视图v)
{
DBAdapter dbAdaptor =新DBAdapter(getApplicationContext()); 尝试
{
dbAdaptor.open(); //串光标= dbAdaptor.getLastOdometer();
。字符串日期= dateEdit.getText()的toString();
串价格= priceEdit.getText()的toString()。
。字符串泵= pumpEdit.getText()的toString();
。字符串tcost = costView.getText()的toString();
。字符串里程表= odometerEdit.getText()的toString();
。字符串FCON = fconView.getText()的toString();
dbAdaptor.insertLog(日期,价格,泵,tcost,里程表,FCON);
意图summaryView =新意图(getApplicationContext(),summary.class);
startActivity(summaryView); }
赶上(例外五){
Log.d(燃油日志,e.getMessage());
}
最后
{
如果(dbAdaptor!= NULL)
dbAdaptor.close();
}
}
}); }
公共静态类DatePickerFragment扩展DialogFragment
实现DatePickerDialog.OnDateSetListener { 公众的EditText EDITTEXT;
ð的DatePicker presult; 公共对话框onCreateDialog(捆绑savedInstanceState){
//使用当前日期作为选择器的默认日期 最后的日历C = Calendar.getInstance();
年整型= c.get(Calendar.YEAR);
INT月= c.get(的Calendar.MONTH);
INT天= c.get(Calendar.DAY_OF_MONTH);
//返回新DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日); //创建DatePickerDialog的新实例,并将其返回
返回新DatePickerDialog(getActivity(),这,年,月,日);
} 公共无效onDateSet(查看的DatePicker,年整型,诠释月,日整型){ dateEdit.setText(将String.valueOf(天)+/
+将String.valueOf(月+ 1)+/+将String.valueOf(年));
//设置选定的日期为日期选择器还
}
}
}
dbadapter.java
公共类DBAdapter { 公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_DATE =日期;
公共静态最后弦乐KEY_PRICE =fuelprice;
公共静态最后弦乐KEY_FUEL =fuelpump;
公共静态最后弦乐KEY_COST =tcost;
公共静态最后弦乐KEY_ODM =里程表;
公共静态最后弦乐KEY_CON =FCON 私有静态最后弦乐TAG =DBAdapter; 私有静态最后弦乐DATABASE_NAME =MYDB;
私有静态最后弦乐DATABASE_TABLE =fuelLog;
私有静态最终诠释DATABASE_VERSION = 2; 私有静态最后弦乐DATABASE_CREATE =
CREATE TABLE fuelLog(_id整数主键自动增量+号的文本不为空,fuelprice文本不为空,fuelpump文本不为空,tcost文本不为空,里程表文本不为空,FCON文字NOT NULL);;
私人最终上下文的背景下; 私人DatabaseHelper DBHelper;
私人SQLiteDatabase分贝; 公共DBAdapter(上下文CTX){
this.context = CTX;
DBHelper =新DatabaseHelper(背景);
} 私有静态类DatabaseHelper扩展SQLiteOpenHelper
{
DatabaseHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
} 公共无效的onCreate(SQLiteDatabase DB)
{
尝试{
db.execSQL(DATABASE_CREATE);
}赶上(的SQLException E){
e.printStackTrace();
}
} //的onCreate 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
Log.w(TAG,从版本升级数据库+ oldVersion +到
+ NEWVERSION +,这将摧毁所有旧数据);
db.execSQL(DROP TABLE IF EXISTS接触);
的onCreate(DB);
} // onUpgrade } // DatabaseHelper
公共DBAdapter的open()抛出的SQLException
{
DB = DBHelper.getWritableDatabase();
返回此;
}//打开
// ---关闭数据库---
公共无效的close()
{
DBHelper.close();
}//关
// ---插入记录到数据库---
众长insertLog(日期字符串,字符串fuelprice,字符串fuelpump,字符串tcost,里程表字符串,字符串FCON)
{
ContentValues initialValues =新ContentValues();
initialValues.put(KEY_DATE,日期);
initialValues.put(KEY_PRICE,fuelprice);
initialValues.put(KEY_FUEL,fuelpump);
initialValues.put(KEY_COST,tcost);
initialValues.put(KEY_ODM,里程表);
initialValues.put(KEY_CON,FCON);
返回db.insert(DATABASE_TABLE,空,initialValues);
} // insertLog 公共字符串getLastOdometer(){
光标光标= db.query(DATABASE_TABLE,新的String [] {} KEY_ODM,KEY_ROWID =(SELECT MAX(KEY_ROWID)FROM+ DATABASE_TABLE +),NULL,NULL,NULL,NULL);
尝试{
如果(cursor.moveToNext()){
返回cursor.getString(0);
}其他{
返回null;
}
} {最后
cursor.close();
}
}}
最后编辑答案
查看解决
进口java.util.Calendar中;进口android.os.Bundle;
进口android.app.Activity;
进口android.app.DatePickerDialog;
进口android.app.Dialog;
进口android.app.DialogFragment;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.DatePicker;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;公共类EditResult1延伸活动{ // TableLayout tablelayout_Log = NULL;
按钮saveButton = NULL;
按钮cancelButton = NULL;
//按钮搜寻] = NULL;
静态的EditText dateEdit;
的EditText priceEdit;
的EditText pumpEdit;
TextView的costView;
的EditText odometerEdit;
TextView的fconView;
TextWatcher textWatcher;
串priceEditStr =,pumpEditStr =;
串odmEditStr =;
串lastOdm =; 双重效果;
双resultCon;
私人诠释mYear;
私人诠释mMonth;
私人诠释MDAY; 共享preferences SP; 静态最终诠释DATE_DIALOG_ID = 0; 公共布尔ISNUMERIC(字符串str)
{
返回str.matches( - \\\\ D +(\\\\ D +)??);
} @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.edit_sample);
costView =(的TextView)findViewById(R.id.tcost);
dateEdit =(EditText上)findViewById(R.id.date);
priceEdit =(EditText上)findViewById(R.id.fuelprice);
pumpEdit =(EditText上)findViewById(R.id.fuelpump);
odometerEdit =(EditText上)findViewById(R.id.odometer);
fconView =(的TextView)findViewById(R.id.fcon); / **
*共享preference
** /
SP = this.getShared preferences(result_store,MODE_WORLD_READABLE);
lastOdm = sp.getString(ODO,lastOdm); 如果(lastOdm.trim()。equalsIgnoreCase(NA))
Toast.makeText(getApplicationContext(),+ lastOdm.trim(),Toast.LENGTH_LONG).show();
其他
Toast.makeText(getApplicationContext(),+ lastOdm.trim(),Toast.LENGTH_LONG).show(); dateEdit.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//的ShowDialog(DATE_DIALOG_ID);
DialogFragment newFragment =新DatePickerFragment();
newFragment.show(getFragmentManager(),日期选择器);
}
});
priceEdit.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效afterTextChanged(编辑编辑){
//这里,当我们在引进的EditText的东西,我们从它的字符串
如果(!。priceEdit.getText()的toString()修剪()equalsIgnoreCase()及&安培;!。priceEdit.getText()的toString()修剪()equalsIgnoreCase(NULL))
priceEditStr = priceEdit.getText()的toString()修剪()。
如果(!。pumpEdit.getText()的toString()修剪()equalsIgnoreCase()及&安培;!。pumpEdit.getText()的toString()修剪()equalsIgnoreCase(NULL))
pumpEditStr = pumpEdit.getText()的toString()修剪()。 如果(!。priceEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器;!pumpEdit.getText()的toString()修剪()equalsIgnoreCase(。))
{
结果= Double.parseDouble(priceEditStr)* Double.parseDouble(pumpEditStr);
costView.setText(+结果);
} }
}); pumpEdit.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效afterTextChanged(编辑编辑){
//这里,当我们在引进的EditText的东西,我们从它的字符串
如果(!priceEdit.getText()。的toString()。修剪()。equalsIgnoreCase())
priceEditStr = priceEdit.getText()的toString()修剪()。
如果(!pumpEdit.getText()。的toString()。修剪()。equalsIgnoreCase())
pumpEditStr = pumpEdit.getText()的toString()修剪()。
如果(!。priceEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器;!pumpEdit.getText()的toString()修剪()equalsIgnoreCase(。))
{
结果= Double.parseDouble(priceEditStr)* Double.parseDouble(pumpEditStr);
costView.setText(+结果);
} }
});
odometerEdit.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I1,I2 INT){ } @覆盖
公共无效afterTextChanged(编辑编辑){
//这里,当我们在引进的EditText的东西,我们从它的字符串 如果(!odometerEdit.getText()。的toString()。修剪()。equalsIgnoreCase())
odmEditStr = odometerEdit.getText()的toString()修剪()。
!。如果(odometerEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器;!pumpEdit.getText()的toString()修剪()equalsIgnoreCase(。)及和放大器; !lastOdm.trim()equalsIgnoreCase()及与放大器;!lastOdm.trim()equalsIgnoreCase(NA))
{ resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(lastOdm)/ Double.parseDouble(pumpEditStr);
fconView.setText(+ resultCon);
} }
}); cancelButton =(按钮)findViewById(R.id.cancelBTN);
cancelButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根 ///有关创建意向,并开始活动
意图summaryView =新意图(getApplicationContext(),ActivityA.class);
startActivity(summaryView);
}
}); //插
saveButton =(按钮)findViewById(R.id.saveBTN);
saveButton.setOnClickListener(新OnClickListener()
{
公共无效的onClick(视图v)
{
DBAdapter dbAdaptor =新DBAdapter(getApplicationContext()); 尝试
{
dbAdaptor.open();
//串光标= dbAdaptor.getLastOdometer();
。字符串日期= dateEdit.getText()的toString();
串价格= priceEdit.getText()的toString()。
。字符串泵= pumpEdit.getText()的toString();
。字符串tcost = costView.getText()的toString();
。字符串里程表= odometerEdit.getText()的toString();
。字符串FCON = fconView.getText()的toString(); 如果(!odometer.trim()。equalsIgnoreCase())
{
共享preferences.Editor编辑= sp.edit();
editor.putString(ODO,odometer.trim());
editor.commit();
}
其他
{
共享preferences.Editor编辑= sp.edit();
editor.putString(ODO,NA);
editor.commit();
} dbAdaptor.insertLog(日期,价格,泵,tcost,里程表,FCON);
意图summaryView =新意图(getApplicationContext(),ActivityA.class);
startActivity(summaryView); }
赶上(例外五){
Log.d(燃油日志,e.getMessage());
}
最后
{
如果(dbAdaptor!= NULL)
dbAdaptor.close();
}
}
}); }
公共静态类DatePickerFragment扩展DialogFragment
实现DatePickerDialog.OnDateSetListener { 公众的EditText EDITTEXT;
ð的DatePicker presult; 公共对话框onCreateDialog(捆绑savedInstanceState){
//使用当前日期作为选择器的默认日期 最后的日历C = Calendar.getInstance();
年整型= c.get(Calendar.YEAR);
INT月= c.get(的Calendar.MONTH);
INT天= c.get(Calendar.DAY_OF_MONTH);
//返回新DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日); //创建DatePickerDialog的新实例,并将其返回
返回新DatePickerDialog(getActivity(),这,年,月,日);
} 公共无效onDateSet(查看的DatePicker,年整型,诠释月,日整型){ dateEdit.setText(将String.valueOf(天)+/
+将String.valueOf(月+ 1)+/+将String.valueOf(年));
//设置选定的日期为日期选择器还
}
}
}
替换EditResult1到您的类名,也同样为XML。
FORMULA: total distance travel(current odometer - previous odometer)/ no. of litres petrol pump
I've done up in my code this formula, however my code was not done completely.My code is (current odometer/ no. of litres petrol).I wanted to minus away the previous odometer , but I don't know how to do so.I will be storing the information in my database, based on the date.Can someone guide me how can I do so?in order to achieve the formula of total distance travel(current odometer - previous odometer)/ no. of litres petrol pump..
this is my code:I know there is something wrong with my String lastOdm=""; , butI not sure what to solve the error.
logcat:
01-22 11:01:12.136: E/AndroidRuntime(7078): FATAL EXCEPTION: main
01-22 11:01:12.136: E/AndroidRuntime(7078): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fuellog/com.example.fuellog.MainActivity}: java.lang.NullPointerException
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.os.Looper.loop(Looper.java:137)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-22 11:01:12.136: E/AndroidRuntime(7078): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 11:01:12.136: E/AndroidRuntime(7078): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 11:01:12.136: E/AndroidRuntime(7078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-22 11:01:12.136: E/AndroidRuntime(7078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-22 11:01:12.136: E/AndroidRuntime(7078): at dalvik.system.NativeStart.main(Native Method)
01-22 11:01:12.136: E/AndroidRuntime(7078): Caused by: java.lang.NullPointerException
01-22 11:01:12.136: E/AndroidRuntime(7078): at com.example.fuellog.DBAdapter.getLastOdometer(DBAdapter.java:104)
01-22 11:01:12.136: E/AndroidRuntime(7078): at com.example.fuellog.MainActivity.onCreate(MainActivity.java:74)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.Activity.performCreate(Activity.java:5104)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-22 11:01:12.136: E/AndroidRuntime(7078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-22 11:01:12.136: E/AndroidRuntime(7078): ... 11 more
mainactivity.java
public class MainActivity extends Activity {
// TableLayout tablelayout_Log = null;
Button saveButton = null;
Button cancelButton = null;
// Button searchButton = null;
static EditText dateEdit;
EditText priceEdit;
EditText pumpEdit;
TextView costView;
EditText odometerEdit;
TextView fconView;
TextWatcher textWatcher;
String priceEditStr ="",pumpEditStr="";
String odmEditStr = "";
String lastOdm = "";
double result;
double resultCon;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
public boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
costView = (TextView)findViewById(R.id.tcost);
dateEdit = (EditText)findViewById(R.id.date);
priceEdit = (EditText)findViewById(R.id.fuelprice);
pumpEdit = (EditText)findViewById(R.id.fuelpump);
odometerEdit = (EditText)findViewById(R.id.odometer);
fconView = (TextView)findViewById(R.id.fcon);
dateEdit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
priceEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
});
pumpEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
});
odometerEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
odmEditStr = odometerEdit.getText().toString().trim();
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase(null) && !lastOdm.trim().equalsIgnoreCase(" "))
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
lastOdm = dbAdaptor.getLastOdometer();
resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(lastOdm) / Double.parseDouble(pumpEditStr);
fconView.setText(" "+resultCon);
}
}
});
cancelButton = (Button) findViewById(R.id.cancelBTN);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for About and start activity
Intent summaryView=new Intent(getApplicationContext(),summary.class);
startActivity(summaryView);
}
});
//Insert
saveButton = (Button) findViewById(R.id.saveBTN);
saveButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
try
{
dbAdaptor.open();
// String cursor = dbAdaptor.getLastOdometer();
String date = dateEdit.getText().toString();
String price = priceEdit.getText().toString();
String pump = pumpEdit.getText().toString();
String tcost = costView.getText().toString();
String odometer = odometerEdit.getText().toString();
String fcon = fconView.getText().toString();
dbAdaptor.insertLog(date, price, pump, tcost, odometer, fcon);
Intent summaryView=new Intent(getApplicationContext(),summary.class);
startActivity(summaryView);
}
catch(Exception e){
Log.d("Fuel Log", e.getMessage());
}
finally
{
if(dbAdaptor != null)
dbAdaptor.close();
}
}
});
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
dateEdit.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}
}
}
dbadapter.java
public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_PRICE = "fuelprice";
public static final String KEY_FUEL = "fuelpump";
public static final String KEY_COST = "tcost";
public static final String KEY_ODM = "odometer";
public static final String KEY_CON = "fcon";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE = "fuelLog";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
"create table fuelLog (_id integer primary key autoincrement, " + "date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
try{
db.execSQL(DATABASE_CREATE);
}catch (SQLException e){
e.printStackTrace();
}
}//onCreate
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}//onUpgrade
}//DatabaseHelper
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}//open
//---closes the database---
public void close()
{
DBHelper.close();
}//close
//---insert a log into the database---
public long insertLog(String date, String fuelprice, String fuelpump,String tcost,String odometer,String fcon )
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_PRICE, fuelprice);
initialValues.put(KEY_FUEL, fuelpump);
initialValues.put(KEY_COST, tcost);
initialValues.put(KEY_ODM, odometer);
initialValues.put(KEY_CON, fcon);
return db.insert(DATABASE_TABLE, null, initialValues);
}//insertLog
public String getLastOdometer() {
Cursor cursor = db.query(DATABASE_TABLE, new String[] {KEY_ODM}, "KEY_ROWID=(SELECT MAX(KEY_ROWID) FROM " + DATABASE_TABLE + ")", null, null, null, null);
try {
if(cursor.moveToNext()) {
return cursor.getString(0);
} else {
return null;
}
} finally {
cursor.close();
}
}
}
Final Edited Answer
See the solved
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class EditResult1 extends Activity {
// TableLayout tablelayout_Log = null;
Button saveButton = null;
Button cancelButton = null;
// Button searchButton = null;
static EditText dateEdit;
EditText priceEdit;
EditText pumpEdit;
TextView costView;
EditText odometerEdit;
TextView fconView;
TextWatcher textWatcher;
String priceEditStr ="",pumpEditStr="";
String odmEditStr = "";
String lastOdm = "";
double result;
double resultCon;
private int mYear;
private int mMonth;
private int mDay;
SharedPreferences sp;
static final int DATE_DIALOG_ID = 0;
public boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_sample);
costView = (TextView)findViewById(R.id.tcost);
dateEdit = (EditText)findViewById(R.id.date);
priceEdit = (EditText)findViewById(R.id.fuelprice);
pumpEdit = (EditText)findViewById(R.id.fuelpump);
odometerEdit = (EditText)findViewById(R.id.odometer);
fconView = (TextView)findViewById(R.id.fcon);
/**
* Shared preference
**/
sp=this.getSharedPreferences("result_store", MODE_WORLD_READABLE);
lastOdm=sp.getString("ODO", lastOdm);
if(lastOdm.trim().equalsIgnoreCase("NA"))
Toast.makeText(getApplicationContext(), " "+lastOdm.trim(), Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), " "+lastOdm.trim(), Toast.LENGTH_LONG).show();
dateEdit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
priceEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
});
pumpEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
});
odometerEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
odmEditStr = odometerEdit.getText().toString().trim();
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase("NA"))
{
resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(lastOdm) / Double.parseDouble(pumpEditStr);
fconView.setText(" "+resultCon);
}
}
});
cancelButton = (Button) findViewById(R.id.cancelBTN);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for About and start activity
Intent summaryView=new Intent(getApplicationContext(),ActivityA.class);
startActivity(summaryView);
}
});
//Insert
saveButton = (Button) findViewById(R.id.saveBTN);
saveButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
try
{
dbAdaptor.open();
// String cursor = dbAdaptor.getLastOdometer();
String date = dateEdit.getText().toString();
String price = priceEdit.getText().toString();
String pump = pumpEdit.getText().toString();
String tcost = costView.getText().toString();
String odometer = odometerEdit.getText().toString();
String fcon = fconView.getText().toString();
if(!odometer.trim().equalsIgnoreCase(""))
{
SharedPreferences.Editor editor=sp.edit();
editor.putString("ODO", odometer.trim());
editor.commit();
}
else
{
SharedPreferences.Editor editor=sp.edit();
editor.putString("ODO", "NA");
editor.commit();
}
dbAdaptor.insertLog(date, price, pump, tcost, odometer, fcon);
Intent summaryView=new Intent(getApplicationContext(),ActivityA.class);
startActivity(summaryView);
}
catch(Exception e){
Log.d("Fuel Log", e.getMessage());
}
finally
{
if(dbAdaptor != null)
dbAdaptor.close();
}
}
});
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
dateEdit.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}
}
}
Replace EditResult1 to your class name and also same for XML .
这篇关于如何减去关闭previous键控值(存储在数据库中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!