我的源代码中有两个值(来自TimePicker按钮的currentHour和currentMinute),我想将它们组合成一个单独的字符串,我可以在xml布局中显示(12:30而不是12和30)我已经能够编译下面的代码但是我被卡住了,它的力量关闭了,我需要一些帮助:我想要做的就是添加一些代码来将currentHour和currentMinute组合成一个字符串,这样我就可以了稍后在xml布局中从TimePicker调用时间。
import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;
public class AddEditCountry extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
private TimePicker minEt;
public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
// minEt = (TimePicker) findViewById(R.id.minEdit);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
timeEt.setCurrentHour(extras.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);
minEt.setCurrentMinute(extras.getInt("min"));
}
Button saveButton =(Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (nameEt.getText().length() != 0)
{
AsyncTask<Object, Object, Object> saveContactTask = new AsyncTask<Object, Object, Object>()
{
@Override
protected Object doInBackground(Object... params)
{
saveContact();
return null;
}
@Override
protected void onPostExecute(Object result)
{
finish();
}
};
saveContactTask.execute((Object[]) null);
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveContact()
{
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
minEt.getCurrentMinute().toString(),
codeEt.getText().toString());
}
else
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
minEt.getCurrentMinute().toString(),/* Storing as String*/
codeEt.getText().toString());
}
}
}
数据库连接器(.java)
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseConnector {
public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";
private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;
public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}
public void open() throws SQLException
{
//open database in reading/writing mode
database = dbOpenHelper.getWritableDatabase();
}
public void close()
{
if (database != null)
database.close();
}
public void insertContact(String name, String cap, String code, String time, String min)
{
ContentValues newCon = new ContentValues();
newCon.put("name", name);
newCon.put("cap", cap);
newCon.put("time", time);
newCon.put("min", min);
newCon.put("code", code);
newCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
newCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);
open();
database.insert("country", null, newCon);
close();
}
public void updateContact(long id, String name, String cap,String code, String time, String min)
{
ContentValues editCon = new ContentValues();
editCon.put("name", name);
editCon.put("cap", cap);
editCon.put("time", time);
editCon.put("min", min);
editCon.put("code", code);
editCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
editCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);
open();
database.update("country", editCon, "_id=" + id, null);
close();
}
public Cursor getAllContacts()
{
return database.query("country", new String[] {"_id", "name"},
null, null, null, null, "name");
}
public Cursor getOneContact(long id)
{
return database.query("country", null, "_id=" + id, null, null, null, null);
}
public void deleteContact(long id)
{
open();
database.delete("country", "_id=" + id, null);
close();
}
}
logcat的:
03-24 00:48:57.779: D/Activity(6809): Activity.onPause(), editTextTapSensorList size: 0
03-24 00:48:57.959: I/Adreno200-EGLSUB(6809): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-24 00:48:57.959: D/memalloc(6809): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:54
03-24 00:48:58.049: D/memalloc(6809): ion: Mapped buffer base:0x5dc22000 size:614400 offset:0 fd:75
03-24 00:48:58.079: D/memalloc(6809): ion: Mapped buffer base:0x5dcb8000 size:614400 offset:0 fd:78
03-24 00:48:58.079: D/memalloc(6809): ion: Unmapping buffer base:0x5d4ce000 size:614400
03-24 00:48:58.079: D/memalloc(6809): ion: Unmapping buffer base:0x5d6f7000 size:614400
03-24 00:48:58.079: D/memalloc(6809): ion: Unmapping buffer base:0x5c929000 size:614400
03-24 00:49:10.623: W/dalvikvm(6809): threadid=12: thread exiting with uncaught exception (group=0x410889d8)
03-24 00:49:10.643: E/AndroidRuntime(6809): FATAL EXCEPTION: AsyncTask #2
03-24 00:49:10.643: E/AndroidRuntime(6809): java.lang.RuntimeException: An error occured while executing doInBackground()
03-24 00:49:10.643: E/AndroidRuntime(6809): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-24 00:49:10.643: E/AndroidRuntime(6809): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.lang.Thread.run(Thread.java:856)
03-24 00:49:10.643: E/AndroidRuntime(6809): Caused by: java.lang.NullPointerException
03-24 00:49:10.643: E/AndroidRuntime(6809): at com.nfc.linkingmanager.AddEditCountry.saveContact(AddEditCountry.java:101)
03-24 00:49:10.643: E/AndroidRuntime(6809): at com.nfc.linkingmanager.AddEditCountry.access$1(AddEditCountry.java:92)
03-24 00:49:10.643: E/AndroidRuntime(6809): at com.nfc.linkingmanager.AddEditCountry$1$1.doInBackground(AddEditCountry.java:65)
03-24 00:49:10.643: E/AndroidRuntime(6809): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-24 00:49:10.643: E/AndroidRuntime(6809): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-24 00:49:10.643: E/AndroidRuntime(6809): ... 5 more
03-24 00:49:10.663: D/Activity(6809): Activity.onPause(), editTextTapSensorList size: 0
03-24 00:49:10.693: I/Adreno200-EGLSUB(6809): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-24 00:49:10.693: D/memalloc(6809): ion: Mapped buffer base:0x5d4cb000 size:614400 offset:0 fd:57
03-24 00:49:10.713: D/memalloc(6809): ion: Mapped buffer base:0x5d8f7000 size:614400 offset:0 fd:68
03-24 00:49:10.753: D/memalloc(6809): ion: Mapped buffer base:0x5e14e000 size:614400 offset:0 fd:72
03-24 00:49:10.753: D/memalloc(6809): ion: Unmapping buffer base:0x5ca41000 size:614400
03-24 00:49:10.753: D/memalloc(6809): ion: Unmapping buffer base:0x5dc22000 size:614400
03-24 00:49:10.753: D/memalloc(6809): ion: Unmapping buffer base:0x5dcb8000 size:614400