我的源代码timeEt(稍后将其重命名为hourEt)和minEt中有两个值,我想将它们组合成一个字符串,可以在xml布局中显示-我已经能够编译代码在下面,但我遇到了麻烦,我需要帮助,我想做的就是添加一些代码以将currentHour和currentMinute合并为一个字符串,以便稍后可以在XML布局中从TimePicker调用时间)
AddEditCountry.java
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)w Bill Motes的有用建议
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();
}
}
添加了Bill Mote的有用建议后,它仍然崩溃。我已经更新了下面的logcat和上面的源代码。我觉得我更接近集成一个字符串,该字符串从TimePicker调用合并的currentHour和currentMinute按钮-但我仍然被卡住(任何StackOverflow成员的任何输入都超过了欢迎!)
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
附言
谢谢大家!
最佳答案
在第一个示例中,您获得的“时间” +“ min”实际上是“ timemin”,但是在第二个示例中,您获得的是“ time”。
所以,有两件事...
public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";
在调用putExtra的类中,将AddEditCountry.KEY_BUNDLE_TIME用作键值对中的键。与AddEditCountry.KEY_BUNDLE_MIN相同。这使被调用的活动/片段控制捆绑中传递的条目,并确保您不创建错别字等。
另外,您可以在执行获取操作之前检查以确保密钥存在。
if (extras.containsKey(KEY_BUNDLE_TIME) {
timeEt.setCurrentHour(extras.getInt(KEY_BUNDLE_TIME));
} else {
timeEt.setCurrentHour(0);
}
要么
timeEt.setCurrentHour(extras.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);