Frage

I want to set kids profile on HomeActivity after user clicks on "Submit" button. I am saving all data in database and it is working. But the application crashes when clicked on "Submit" button. Data is not passed to HomeActivity from 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)
War es hilfreich?

Lösung

        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");
}

Problem is to get intent line:

Bundle myIntent = getIntent().getExtras();

instead of

myIntent = getIntent();

i hope its useful to you.

Andere Tipps

You have to getExtras from intent. Try getIntent().getExtras() and proceed.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top