我的申请随着我打开第四个活动而下降

时间:2018-01-04 10:58:51

标签: java android android-activity nullpointerexception

我试图自己解决这个问题,但我很生气,因为不明白为什么它不起作用。我开发了非常简单的应用程序,用户需要回答大约10个问题并且程序接受输入计算并根据他的输入返回推荐。它从一个活动传递到另一个活动(答案保存在xml的文件中),第四个活动将所有答案转换为java,在这个java文件中我进行计算。但是当我从活动转到另一个活动时,一切正常,直到最后一次活动,我甚至没有开始计算。在转换步骤中,我做了模拟器,任何时候它的下降。我试图删除RadioButton转换器并且它的工作正常,但据我所知,我将RadioButton转换为好。

我的javaFile代码(最后一个活动):

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_last);


        //declare RadioGroup
        sort_b_r_g=(RadioGroup) findViewById(R.id.sort_b_r_g);
        winter_r_g=(RadioGroup) findViewById(R.id.winter_r_g);
        stand_r_g=(RadioGroup) findViewById(R.id.stand_r_g);
        corner_r_g=(RadioGroup) findViewById(R.id.corner_r_g);
        sex_r_g=(RadioGroup) findViewById(R.id.sex_r_g);

        //declare RadioButton.... the problem is here.

        sort_b_r_b=(RadioButton) findViewById(sort_b_r_g.getCheckedRadioButtonId());
        winter_r_b=(RadioButton) findViewById(winter_r_g.getCheckedRadioButtonId());
        stand_r_b=(RadioButton) findViewById(stand_r_g.getCheckedRadioButtonId());
        corner_r_b=(RadioButton) findViewById(corner_r_g.getCheckedRadioButtonId());
        sex_r_b=(RadioButton) findViewById(sex_r_g.getCheckedRadioButtonId());

}
}

非常感谢帮助者:)!

关于响应者,申请刚刚落下...... 错误: “ApplicatioName已停止。 再次打开应用程序“

logcat错误:

01-05 07:47:42.389 5213-5213/? I/zygote: Not late-enabling -Xcheck:jni (already on)
01-05 07:47:42.425 5213-5213/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
01-05 07:47:42.840 5213-5213/com.example.yagel.twoactivity I/InstantRun: starting instant run server: is main process
01-05 07:47:43.572 5213-5239/com.example.yagel.twoactivity D/OpenGLRenderer: HWUI GL Pipeline

                                                                             [ 01-05 07:47:43.641  5213: 5239 D/         ]
                                                                             HostConnection::get() New Host Connection established 0x966c00c0, tid 5239
01-05 07:47:43.650 5213-5239/com.example.yagel.twoactivity I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
01-05 07:47:43.650 5213-5239/com.example.yagel.twoactivity I/OpenGLRenderer: Initialized EGL, version 1.4
01-05 07:47:43.650 5213-5239/com.example.yagel.twoactivity D/OpenGLRenderer: Swap behavior 1
01-05 07:47:43.651 5213-5239/com.example.yagel.twoactivity W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
01-05 07:47:43.651 5213-5239/com.example.yagel.twoactivity D/OpenGLRenderer: Swap behavior 0
01-05 07:47:43.666 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglCreateContext: 0xa9d852a0: maj 2 min 0 rcv 2
01-05 07:47:43.704 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:43.847 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:49.596 5213-5218/com.example.yagel.twoactivity I/zygote: Do partial code cache collection, code=29KB, data=30KB
01-05 07:47:49.600 5213-5218/com.example.yagel.twoactivity I/zygote: After code cache collection, code=29KB, data=30KB
01-05 07:47:49.600 5213-5218/com.example.yagel.twoactivity I/zygote: Increasing code cache capacity to 128KB
01-05 07:47:49.722 5213-5218/com.example.yagel.twoactivity I/zygote: Do partial code cache collection, code=29KB, data=51KB
01-05 07:47:49.723 5213-5218/com.example.yagel.twoactivity I/zygote: After code cache collection, code=29KB, data=51KB
01-05 07:47:49.723 5213-5218/com.example.yagel.twoactivity I/zygote: Increasing code cache capacity to 256KB
01-05 07:47:49.723 5213-5218/com.example.yagel.twoactivity I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
01-05 07:47:49.723 5213-5218/com.example.yagel.twoactivity I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
01-05 07:47:49.797 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:49.812 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:49.832 5213-5218/com.example.yagel.twoactivity I/zygote: Do full code cache collection, code=110KB, data=63KB
01-05 07:47:49.832 5213-5218/com.example.yagel.twoactivity I/zygote: After code cache collection, code=91KB, data=44KB
01-05 07:47:50.062 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:50.159 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:50.236 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:47:50.316 5213-5213/com.example.yagel.twoactivity V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741851
01-05 07:47:50.329 5213-5213/com.example.yagel.twoactivity I/AssistStructure: Flattened final assist data: 8332 bytes, containing 1 windows, 29 views
01-05 07:47:53.936 5213-5218/com.example.yagel.twoactivity I/zygote: Do partial code cache collection, code=123KB, data=78KB
01-05 07:47:53.940 5213-5218/com.example.yagel.twoactivity I/zygote: After code cache collection, code=123KB, data=78KB
01-05 07:47:53.940 5213-5218/com.example.yagel.twoactivity I/zygote: Increasing code cache capacity to 512KB
01-05 07:48:03.404 5213-5218/com.example.yagel.twoactivity I/zygote: Do full code cache collection, code=248KB, data=171KB
01-05 07:48:03.413 5213-5218/com.example.yagel.twoactivity I/zygote: After code cache collection, code=245KB, data=131KB
01-05 07:48:03.517 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:48:03.575 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:48:03.685 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:48:03.742 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:48:03.787 5213-5213/com.example.yagel.twoactivity V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741840
01-05 07:48:03.793 5213-5213/com.example.yagel.twoactivity I/AssistStructure: Flattened final assist data: 5020 bytes, containing 1 windows, 18 views
01-05 07:48:03.829 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:48:04.169 5213-5239/com.example.yagel.twoactivity D/EGL_emulation: eglMakeCurrent: 0xa9d852a0: ver 2 0 (tinfo 0xa9d83280)
01-05 07:48:04.893 5213-5218/com.example.yagel.twoactivity I/zygote: Do partial code cache collection, code=247KB, data=147KB
01-05 07:48:04.893 5213-5218/com.example.yagel.twoactivity I/zygote: After code cache collection, code=247KB, data=147KB
01-05 07:48:04.893 5213-5218/com.example.yagel.twoactivity I/zygote: Increasing code cache capacity to 1024KB
01-05 07:48:26.207 5213-5213/com.example.yagel.twoactivity D/AndroidRuntime: Shutting down VM
01-05 07:48:26.208 5213-5213/com.example.yagel.twoactivity E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.yagel.twoactivity, PID: 5213
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yagel.twoactivity/com.example.yagel.twoactivity.Last}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                                 at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                 at android.os.Looper.loop(Looper.java:164)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
                                                                                 at com.example.yagel.twoactivity.Last.onCreate(Last.java:42)
                                                                                 at android.app.Activity.performCreate(Activity.java:6999)
                                                                                 at android.app.Activity.performCreate(Activity.java:6990)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                                 at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                 at android.os.Looper.loop(Looper.java:164) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yagel.twoactivity.Last">

    <ImageView
        android:id="@+id/soft_im"
        android:layout_width="340dp"
        android:layout_height="250dp"
        android:layout_marginTop="50dp"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/soft" />

    <ImageView
        android:id="@+id/long_im"
        android:layout_width="340dp"
        android:layout_height="250dp"
        android:layout_marginTop="50dp"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/long_b" />

    <ImageView
        android:id="@+id/fish_im"
        android:layout_width="340dp"
        android:layout_height="250dp"
        android:layout_marginTop="50dp"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/fish" />

    <ImageView
        android:id="@+id/short_im"
        android:layout_width="340dp"
        android:layout_height="250dp"
        android:layout_marginTop="50dp"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/short_b" />

    <TextView
        android:id="@+id/ans"
        android:layout_width="256dp"
        android:layout_height="43dp"
        android:layout_marginBottom="140dp"
        android:text="כנראה שלא מלאת את כל הפרטים.\nיש לחזור על התהליך ולמלא את כל הפרטים"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
好的,所以我发现我的问题,只要应用程序意图到另一个项目,R.id.id_name就会变为null。 因此,当我试图在scound活动(从第四个)开始给出一些可靠的命令时,应用程序从中得到了一个空值。 所以通过制作代表R.id.id_name中的信息的可验证的“公共静态”来解决它(例如:我有TextView“id_name”,所以在第二个活动中我初始化String name_s并且做了name_s = id_name.gettext()然后,当开放新活动时,可靠的没有改变..) 该应用程序运行良好,如果你有另一种技术比我的更好,我会很乐意帮助它,非常感谢帮助者:)

1 个答案:

答案 0 :(得分:0)

它只是另一个NullPointerException。您在null上使用getCheckedRadioButtonId()。修复它,它会工作。记录您的代码并检查您的单选按钮为空的原因。