SQLite 数据库帮助

发布于 2024-11-08 02:19:25 字数 3194 浏览 1 评论 0原文

我在创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2024-11-15 02:19:25

在尝试从结果意图中获取额外内容之前,您应该在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文