通过意图点击按钮将数据发送到其他活动

时间:2014-05-15 06:53:23

标签: android sqlite android-intent

我想在用户点击“提交”按钮后在HomeActivity设置儿童资料。我正在保存数据库中的所有数据,它正在工作。但单击“提交”按钮时应用程序崩溃。数据不会从HomeActivity传递到KidsProfileActivity

KidsProfileActivity.java

btnSubmit.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        strKidsName = edtKidsName.getText().toString();
        strKidsBirthDate = edtKidsBirthDate.getText().toString();
        strKidsBirthTime = edtKidsBirthTime.getText().toString();
        strKidsWeight = edtKidsWeight.getText().toString();
        strKidsHeight = edtKidsHeight.getText().toString();
        strKidsHeadCircumference = edtKidsHeadCircumference.getText()
                .toString();
        strHealthStatus = spHealthStatus.getSelectedItem().toString();

        if (rbtnBoy.isChecked()) {
            strGenderValue = "Boy";
        } else {
            strGenderValue = "Girl";
        }

        if (strKidsName.equals("")) {
            ting("Please Enter Kids");
        } else if (strKidsBirthDate.equals("")) {
            ting("Please Enter BirthDate");
        } else if (strKidsBirthTime.equals("")) {
            ting("Please Enter BirthTime");
        } else if (strKidsHeight.equals("")) {
            ting("Please Enter Weight");
        } else if (strKidsWeight.equals("")) {
            ting("Please Enter Weight");
        } else if (strKidsHeadCircumference.equals("")) {
            ting("Please Enter HeadCircumference");
        }

        else if (strHealthStatus.equals("")) {
            ting("Please Select HealthStatus");
        }

        else {
            masterDatabaseAdapter.kidsProfileEntry(strKidsName,
                    strKidsBirthDate, strKidsBirthTime, strGenderValue,
                    strKidsHeight, strKidsWeight,
                    strKidsHeadCircumference, strHealthStatus);
            Intent displaykidsProfile = new Intent(
                    KidsProfileActivity.this, HomeActivity.class);

            displaykidsProfile.putExtra("kidsname", strKidsName);
            displaykidsProfile.putExtra("birthdate", strKidsBirthDate);
            displaykidsProfile.putExtra("birthtime", strKidsBirthTime);
            displaykidsProfile.putExtra("height", strKidsHeight);
            displaykidsProfile.putExtra("weight", strKidsWeight);
            displaykidsProfile.putExtra("head",
                    strKidsHeadCircumference);
            displaykidsProfile.putExtra("health", strHealthStatus);

            startActivity(displaykidsProfile);
        }

HomeActivity.java

myIntent = getIntent();
if (myIntent.hasExtra("kidsname") && myIntent.hasExtra("birthdate")
        && myIntent.hasExtra("birthtime")
        && myIntent.hasExtra("height") && myIntent.hasExtra("weight")&& myIntent.hasExtra("head")&& myIntent.hasExtra("health")) 
{

    tvKidsName.setText(myIntent.getStringExtra("kidsname"));
    tvKidsBirthDate.setText(myIntent.getStringExtra("birthdate"));
    tvKidsBirthTime.setText(myIntent.getStringExtra("birthtime"));
    tvKidsHeight.setText(myIntent.getStringExtra("height"));
    tvKidsWeight.setText(myIntent.getStringExtra("weight"));
    tvKidsHeadCircumference.setText(myIntent.getStringExtra("head"));
    tvHealthStatus.setText(myIntent.getStringExtra("health"));

} else {
    showAlert("Enter Kids Profile First");
}

logcat的

05-15 11:28:22.016: E/AndroidRuntime(1508): FATAL EXCEPTION: main
05-15 11:28:22.016: E/AndroidRuntime(1508): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidingmenuexample/com.example.kidsfinal.HomeActivity}: java.lang.NullPointerException
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.os.Looper.loop(Looper.java:137)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at java.lang.reflect.Method.invoke(Method.java:511)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at dalvik.system.NativeStart.main(Native Method)
05-15 11:28:22.016: E/AndroidRuntime(1508): Caused by: java.lang.NullPointerException
05-15 11:28:22.016: E/AndroidRuntime(1508):     at com.example.kidsfinal.HomeActivity.initComponent(HomeActivity.java:74)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at com.example.kidsfinal.HomeActivity.onCreate(HomeActivity.java:37)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.Activity.performCreate(Activity.java:5008)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-15 11:28:22.016: E/AndroidRuntime(1508):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

2 个答案:

答案 0 :(得分:1)

        Bundle myIntent = getIntent().getExtras();

if (myIntent.hasExtra("kidsname") && myIntent.hasExtra("birthdate")
        && myIntent.hasExtra("birthtime")
        && myIntent.hasExtra("height") && myIntent.hasExtra("weight")&& myIntent.hasExtra("head")&& myIntent.hasExtra("health")) 
{

    tvKidsName.setText(myIntent.getStringExtra("kidsname"));
    tvKidsBirthDate.setText(myIntent.getStringExtra("birthdate"));
    tvKidsBirthTime.setText(myIntent.getStringExtra("birthtime"));
    tvKidsHeight.setText(myIntent.getStringExtra("height"));
    tvKidsWeight.setText(myIntent.getStringExtra("weight"));
    tvKidsHeadCircumference.setText(myIntent.getStringExtra("head"));
    tvHealthStatus.setText(myIntent.getStringExtra("health"));

} else {
    showAlert("Enter Kids Profile First");
}

问题是获得意图线:

捆绑myIntent = getIntent()。getExtras();

而不是

myIntent = getIntent();

我希望它对你有用。

答案 1 :(得分:0)

你必须从意图中获取。尝试     getIntent()。getExtras()并继续。