SQLite 数据库帮助
我在创建 sql 表时遇到问题。我有一个列表视图,应该存储标题和另一个获取信息并将其发送到 ActivityResult 的活动。
这是 MyDatabaseHelper 类中的 createFavorite 方法。
public long createFavorite(String title, int lon , int lat) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_LAT, lat);
initialValues.put(KEY_LON, lon);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
这就是我获取信息并将其作为捆绑包传递的地方。
public class SaveFav extends Activity {
EditText textBox;
Button saveAs;
LocationListener loc;
LocationManager locMng;
MyLocationOverlay myLoc;
GeoPoint p = null;
String s;
int lat = 0;
int lon = 0;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.savelocation);
textBox = (EditText) findViewById(R.id.edittext);
saveAs = (Button) findViewById(R.id.save);
loc = new MyLocationListener();
locMng = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMng.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
saveAs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (p == null) {
Toast.makeText(getBaseContext(), "Can not get GPS Signal",
Toast.LENGTH_LONG).show();
} else {
Bundle b = new Bundle();
b.putString(MyDatabaseHelper.KEY_TITLE, textBox.getText().toString());
b.putInt(MyDatabaseHelper.KEY_LAT, p.getLatitudeE6());
b.putInt(MyDatabaseHelper.KEY_LON, p.getLongitudeE6());
Intent i = new Intent();
i.putExtras(b);
setResult(RESULT_OK, i);
finish();
}
}
});
}
然后我在另一项活动中获取结果。
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle b = intent.getExtras();
switch(requestCode){
case ACTIVITY_CREATE:
String title = b.getString(MyDatabaseHelper.KEY_TITLE);
int lat = b.getInt(MyDatabaseHelper.KEY_LAT);
int lon = b.getInt(MyDatabaseHelper.KEY_LON);
Toast.makeText(getBaseContext(), "" + lat , Toast.LENGTH_LONG).show();
//mDbHelper.createFavorite(title,lat ,lon);
break;
}
这是我的错误代码。
05-17 19:42:46.027: ERROR/AndroidRuntime(398): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.state.park/com.state.park.Favorites}: java.lang.NullPointerException
05-17 19:42:46.027: ERROR/AndroidRuntime(398): at com.state.park.Favorites.onActivityResult(Favorites.java:150)
I am having problems with creating the sql table. I have a listview that should store titles and another activity that grabs the information and sends it onActivityResult.
This is the createFavorite method in MyDatabaseHelper class.
public long createFavorite(String title, int lon , int lat) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_LAT, lat);
initialValues.put(KEY_LON, lon);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
and this is where I grab the info and pass it as a bundle.
public class SaveFav extends Activity {
EditText textBox;
Button saveAs;
LocationListener loc;
LocationManager locMng;
MyLocationOverlay myLoc;
GeoPoint p = null;
String s;
int lat = 0;
int lon = 0;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.savelocation);
textBox = (EditText) findViewById(R.id.edittext);
saveAs = (Button) findViewById(R.id.save);
loc = new MyLocationListener();
locMng = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMng.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
saveAs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (p == null) {
Toast.makeText(getBaseContext(), "Can not get GPS Signal",
Toast.LENGTH_LONG).show();
} else {
Bundle b = new Bundle();
b.putString(MyDatabaseHelper.KEY_TITLE, textBox.getText().toString());
b.putInt(MyDatabaseHelper.KEY_LAT, p.getLatitudeE6());
b.putInt(MyDatabaseHelper.KEY_LON, p.getLongitudeE6());
Intent i = new Intent();
i.putExtras(b);
setResult(RESULT_OK, i);
finish();
}
}
});
}
then I grab the result in another activity.
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle b = intent.getExtras();
switch(requestCode){
case ACTIVITY_CREATE:
String title = b.getString(MyDatabaseHelper.KEY_TITLE);
int lat = b.getInt(MyDatabaseHelper.KEY_LAT);
int lon = b.getInt(MyDatabaseHelper.KEY_LON);
Toast.makeText(getBaseContext(), "" + lat , Toast.LENGTH_LONG).show();
//mDbHelper.createFavorite(title,lat ,lon);
break;
}
this is my error code.
05-17 19:42:46.027: ERROR/AndroidRuntime(398): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.state.park/com.state.park.Favorites}: java.lang.NullPointerException
05-17 19:42:46.027: ERROR/AndroidRuntime(398): at com.state.park.Favorites.onActivityResult(Favorites.java:150)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试从结果意图中获取额外内容之前,您应该在 onActivityResult 方法中检查结果是否实际上为 RESULT_OK。
You should check that the result is actually RESULT_OK in your onActivityResult method before trying to get the extras from the result intent.