Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过单击按钮将数据发送到其他活动_Android_Sqlite_Android Intent - Fatal编程技术网

Android 通过单击按钮将数据发送到其他活动

Android 通过单击按钮将数据发送到其他活动,android,sqlite,android-intent,Android,Sqlite,Android Intent,在用户单击“提交”按钮后,我想在HomeActivity上设置儿童档案。我正在保存数据库中的所有数据,它正在工作。但当点击“提交”按钮时,应用程序崩溃。数据未从kidsprofiliactivity传递到HomeActivity KidsProfileActivity.java btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) {

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

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);
        }
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");
}
HomeActivity.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);
        }
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)
问题是要获得目的线:

Bundle myIntent=getIntent().getExtras()

而不是

myIntent=getIntent()


我希望它对你有用。

你必须从intent获得额外的信息。尝试
getIntent().getExtras()并继续。

您可以发布您的logcat输出吗?@nurisezgin:-posted您是否为btnSubmit声明了正确的id?i、 e
btnSubmit=(按钮)findviewbyd(R.id.your_id)ting
?放置调试器并调试应用程序,这是一个简单的空指针异常。问题在于HomeActivity.java中的第74行。如果您正在使用eclipse,请遵循以下非常重要的基本编程技巧:您确实需要学习调试,特别是对于新用户。了解问题的原因是解决问题的一半。