【问题标题】:Why doesn't try-Catch work as it's supposed to be?为什么 try-Catch 不能正常工作?
【发布时间】:2017-08-14 20:52:11
【问题描述】:

我想做一个 try-catch 块。

在我的 try 块中,有一个方法检查是否按下了 radioButton,如果是,它应该设置一个枚举并继续前进,如果没有按下 radioButton,则应该抛出一个 NullPointerException 并关闭应用程序(还有一些额外的功能尚未完成,所以我暂时删除了它们)。

到目前为止我看到的一切都不适合我的问题,因为他们只是忘记了一个问题,或者他们拼错了一个方法或类似的东西。

如果我不按radioButton 我的应用程序关闭,它会工作,但如果我按radioButton 我的应用程序会再次关闭,但它应该转到我的下一个布局并设置一个枚举(取决于哪个@987654326 @被按下。

我的java代码:

   public void setKnowledge() {
    setContentView(R.layout.knowledge_level);

    Button checking = (Button) findViewById(R.id.buttonChecking);

    checking.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            try {
                checkSelection();
                setContentView(R.layout.category_auswahl);
            }catch(NullPointerException n) {
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            }
        }
    });
}

public knowledge checkSelection() {
    RadioButton rbNoob = (RadioButton) findViewById(R.id.radioButtonNoob);
    RadioButton rbBeginner = (RadioButton) findViewById(R.id.radioButtonBeginner);
    RadioButton rbAdvanced = (RadioButton) findViewById(R.id.radioButtonAdv);
    RadioButton rbPro = (RadioButton) findViewById(R.id.radioButtonPro);
    RadioButton rbGrandMaster = (RadioButton) findViewById(R.id.radioButtonGM);

    advertisment();

    if(rbNoob.isChecked()) {
        return knowledge.NOOB;
    }
    else if(rbBeginner.isChecked()) {
        return knowledge.BEGINNER;
    }
    else if(rbAdvanced.isChecked()) {
        return knowledge.ADVANCED;
    }
    else if(rbPro.isChecked()) {
        return knowledge.PRO;
    }
    else if(rbGrandMaster.isChecked()) {
        return knowledge.GM;
    }
    return null;
}

我的 XML:(编辑:knowledge_level.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"
android:background="#081929" >

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/testWissenstandText"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:id="@+id/radioGroup"
    app:layout_constraintRight_toLeftOf="@+id/buttonChecking"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.538">

    <RadioButton
        android:id="@+id/radioButtonNoob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:checked="false"
        android:text="Keine Vorkenntnisse"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/buttonChecking"
        app:layout_constraintTop_toBottomOf="@+id/testWissenstandText"/>

    <RadioButton
        android:id="@+id/radioButtonBeginner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:checked="false"
        android:text="Anfänger"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonNoob"
        app:layout_constraintRight_toRightOf="@+id/radioButtonNoob"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonNoob"/>

    <RadioButton
        android:id="@+id/radioButtonAdv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:text="Fortgeschrittener"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonBeginner"
        app:layout_constraintRight_toRightOf="@+id/radioButtonBeginner"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonBeginner"/>

    <RadioButton
        android:id="@+id/radioButtonPro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:text="Pro"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonAdv"
        app:layout_constraintRight_toRightOf="@+id/radioButtonAdv"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonAdv"/>

    <RadioButton
        android:id="@+id/radioButtonGM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:text='"Grand Master"'
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonPro"
        app:layout_constraintRight_toRightOf="@+id/radioButtonPro"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonPro"/>

</RadioGroup>

<ImageView
    android:id="@+id/kastenKnowledge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    app:layout_constraintHorizontal_bias="0.517"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/kasten"/>

<TextView
    android:id="@+id/testWissenstandHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Dein Wissensstand?"
    android:textAlignment="center"
    android:textColor="#511613"
    android:textSize="32sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/kastenKnowledge"
    app:layout_constraintLeft_toLeftOf="@+id/kastenKnowledge"
    app:layout_constraintRight_toRightOf="@+id/kastenKnowledge"
    app:layout_constraintTop_toTopOf="@+id/kastenKnowledge"/>

<TextView
    android:id="@+id/testWissenstandText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="24dp"
    android:text='Wie würdest du deinen Wissenstand\nbezüglich dem Spieleinhalt\nvon "Overwatch" beurteilen?'
    android:textAlignment="center"
    android:textColor="#E0E0E0"
    android:textSize="20sp"
    app:layout_constraintHorizontal_bias="0.512"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/testWissenstandHeader"/>

<Button
    android:id="@+id/buttonChecking"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="0dp"
    android:background="@drawable/button_box"
    android:text="C\nL\nI\nC\nK"
    android:textAllCaps="false"
    android:textColor="#511613"
    android:textSize="16sp"
    android:textStyle="bold"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="@+id/radioGroup"
    android:layout_marginTop="8dp"
    app:layout_constraintVertical_bias="0.5"/>



<TextView
    android:id="@+id/knowledgeHint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:text="Wenn du einen Wissenstand\nfestgelegt hast, drücke den\nKnopf daneben!"
    android:textAlignment="center"
    android:textColor="#FF4400"
    android:textSize="20sp"
    android:textStyle="bold|italic"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/radioGroup"/>

</android.support.constraint.ConstraintLayout>

错误信息:

08-14 21:01:26.469 4312-4312/? I/Process: Sending signal. PID: 4312 SIG: 9
08-14 21:01:26.470 1521-1590/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client

                                                      --------- beginning of system
08-14 21:01:26.476 1521-1773/system_process I/WindowState: WIN DEATH: Window{ab30a43 u0 com.example.alexander.hashtagnevereverbuggy/com.example.alexander.hashtagnevereverbuggy.MainActivity}
08-14 21:01:26.480 1521-2538/system_process I/ActivityManager: Process com.example.alexander.hashtagnevereverbuggy (pid 4312) has died
08-14 21:01:26.480 1521-2538/system_process W/ActivityManager: Force removing ActivityRecord{ed88b90 u0 com.example.alexander.hashtagnevereverbuggy/.MainActivity t137}: app died, no saved state
08-14 21:01:26.491 1128-1128/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
08-14 21:01:27.104 1776-1958/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0x7fd4ff628200: ver 2 0
08-14 21:01:27.135 1521-1773/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 4312 uid 10058

【问题讨论】:

  • 您发布的布局是哪个布局..?
  • 你在问为什么你的代码抛出 NPE?
  • @MuthukrishnanRajendran 哦,是的。对不起。我编辑了它
  • 在哪里抛出 NPE?向我们展示您的堆栈跟踪。
  • 这不是问题所在。堆栈跟踪应该向您显示哪一行引发了异常,因此我们不必猜测问题。

标签: java android if-statement enums nullpointerexception


【解决方案1】:

删除 onclick 监听器中的setContentView(R.layout.category_auswahl);

【讨论】:

  • 如果没有这种方法,如何将布局更改为下一个(category_auswahl)?
  • 你不应该这样设置,那个布局里有什么..?
  • 你的布局需要在同一个活动中吗?
  • 有 4 个 ImageButtons、13 个 textViews、4 个 ImageViews、1 个 adView
  • @Lunkie 你是什么意思?
【解决方案2】:

变化:

public knowledge checkSelection() {
    RadioButton rbNoob = (RadioButton) findViewById(R.id.radioButtonNoob);
    RadioButton rbBeginner = (RadioButton) findViewById(R.id.radioButtonBeginner);

    // etc

    else if(rbGrandMaster.isChecked()) {
        return knowledge.GM;
    }
    return null;
}

到这里:

public knowledge checkSelection() {
    RadioButton rbNoob = (RadioButton) findViewById(R.id.radioButtonNoob);
    RadioButton rbBeginner = (RadioButton) findViewById(R.id.radioButtonBeginner);

    // etc

    else if(rbGrandMaster.isChecked()) {
        return knowledge.GM;
    }
    throw new NullPointerException();
}

【讨论】:

    【解决方案3】:
        try {
                if (checkSelection() == null) {
                throw new NullPointerException("checkSelection is null");
                   }
                setContentView(R.layout.category_auswahl);
            }catch(NullPointerException n) {
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            }
    

    【讨论】:

      【解决方案4】:

      伙计,在这段代码中,try-catch 不是你的问题。

      它工作正常并关闭您的应用程序: 08-14 21:01:26.469 4312-4312/? I/Process: Sending signal. PID: 4312 SIG: 9

      您没有打印 NPE,这就是您在日志中没有它的原因。

      您需要使用 Fragments 而不是设置新的内容视图。

      您永远不需要在 Android 中使用 System.exit(0),因为它违反了系统,您必须使用 Activity 类中的 finish

      您可以将 onClickListener 设置到 RadioGroup 中,而不是使用检查按钮,每次进行选择时都会收到事件,因此您可以继续下一个屏幕(活动或片段,但没有理由使用另一个设置内容视图)。

      这里是关于 setContentView 的很好的解释:Android: is using setContentView multiple times bad while changing layouts?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-13
        • 2011-12-25
        • 2013-04-15
        • 1970-01-01
        • 2017-10-22
        • 2016-07-16
        相关资源
        最近更新 更多