使用来自不同活动的多个intent-extra

时间:2012-11-08 21:42:28

标签: android database android-intent

我在两个不同的活动中使用intent-extra,然后使用ContentBalue使用单个包来存储它们,但它显示我的错误....这里是我使用内容值的活动代码...

    package com.example.alert;

    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class StoringEntire extends Activity {
     DatabaseHelper db=new DatabaseHelper(this);
     public static String alerterName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.storing_entire);
    Button save=(Button) findViewById(R.id.save);

    save.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bundle bundle=getIntent().getExtras();
            alerterName=bundle.getString("alerterName");
            double lat=bundle.getDouble("Latitude");
            double lon=bundle.getDouble("Longitude");
            SQLiteDatabase datab=db.getWritableDatabase();
            ContentValues cv=new ContentValues();
            cv.put(db.KEY_ALERT, alerterName);
            cv.put(db.KEY_LATITUDE,lat);
            cv.put(db.KEY_LONGITUDE, lon);
            datab.insert("data",db.KEY_ALERT,cv);
            datab.insert("data",db.KEY_LATITUDE,cv);
            datab.insert("data",db.KEY_LONGITUDE,cv);
            db.close();

        }
    });
}

}

- 11-09 03:17:23.939: E/AndroidRuntime(1012): FATAL EXCEPTION: main </n>
- 11-09 03:17:23.939: E/AndroidRuntime(1012): java.lang.NullPointerException
- 11-09 03:17:23.939: E/AndroidRuntime(1012): at com.example.alert.StoringEntire$1.onClick(StoringEntire.java:26)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at android.view.View.performClick(View.java:4084)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at android.view.View$PerformClick.run(View.java:16966)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at android.os.Handler.handleCallback(Handler.java:615)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at android.os.Handler.dispatchMessage(Handler.java:92)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at android.os.Looper.loop(Looper.java:137)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at android.app.ActivityThread.main(ActivityThread.java:4745)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at java.lang.reflect.Method.invokeNative(Native Method)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at java.lang.reflect.Method.invoke(Method.java:511)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
- 11-09 03:17:23.939: E/AndroidRuntime(1012):   at dalvik.system.NativeStart.main(Native Method)

这些是我正在使用的意图

    Intent myIntent = new Intent(AddLocation.this, AddTrigger.class);
           myIntent.putExtra("Latitude", ""+ destLatitude);
       myIntent.putExtra("Longitude",""+ destLongitude);

还有其他活动

    Intent Inten = new Intent(AddAlerter.this, AddLocation.class);
                 Inten.putExtra("alerterName", alerterNameEditText.getText().toString());

                startActivity(Inten);

1 个答案:

答案 0 :(得分:0)

您需要检查代码中的null值,并且您要调用insert()三次,请尝试以下操作:

public class StoringEntire extends Activity {
    DatabaseHelper db;
    public static String alerterName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.storing_entire);

        db = new DatabaseHelper(this);
        Button save=(Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(getIntent() != null) {
                    Bundle bundle=getIntent().getExtras();
                    if(bundle != null) {
                        alerterName=bundle.getString("alerterName");
                        double lat=bundle.getDouble("Latitude");
                        double lon=bundle.getDouble("Longitude");
                        SQLiteDatabase datab=db.getWritableDatabase();
                        ContentValues cv=new ContentValues();
                        cv.put(db.KEY_ALERT, alerterName);
                        cv.put(db.KEY_LATITUDE,lat);
                        cv.put(db.KEY_LONGITUDE, lon);
                        datab.insert("data", null, cv);
                        db.close();
                    }
                }
            }
        });
    }
}