简体   繁体   English

为什么try-Catch无法按预期工作?

[英]Why doesn't try-Catch work as it's supposed to be?

I wanted to make a try-catch block. 我想做一个try-catch块。

In my try block there's a method which checks if a radioButton is pressed, if so it should set an enum and go forward and if no radioButton is pressed, a NullPointerException should be thrown and the app closes (there're coming some extra features which aren't finished yet so I removed them for now). 在我的try块中,有一个方法可以检查是否按下了radioButton ,如果这样,它应该设置一个枚举并前进,如果没有按下radioButton ,则应引发NullPointerException并关闭应用程序(其中有一些额外的功能,还没有完成,所以我暂时将其删除了)。

Everything I saw so far didnt fit to my problem because they just forgot a catch or they missspelled a method or something like this. 到目前为止,我看到的所有内容都不适合我的问题,因为他们只是忘了一个陷阱,或者他们拼错了一种方法或类似的东西。

It works if I don't press a radioButton my app closes, but if I press one radioButton my app closes again, but it should go to my next layout and set an enum (depending on which radioButton is pressed. 如果我不按我的应用程序关闭一个radioButton它会起作用,但是如果我按一个radioButton我的应用程序再次关闭,但是它应该转到我的下一个布局并设置一个枚举(取决于按下哪个radioButton

My java-code: 我的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;
}

My XML: (EDIT: knowledge_level.xml) 我的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>

Error message(s): 错误消息:

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

Remove setContentView(R.layout.category_auswahl); 删除setContentView(R.layout.category_auswahl); inside onclick listener. 内部onclick侦听器。

Change: 更改:

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

To this: 对此:

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

Man, try-catch is not your problem in this code. 伙计,try-catch不是您在此代码中遇到的问题。

It works fine and closes your app: 08-14 21:01:26.469 4312-4312/? I/Process: Sending signal. PID: 4312 SIG: 9 它工作正常并关闭您的应用: 08-14 21:01:26.469 4312-4312/? I/Process: Sending signal. PID: 4312 SIG: 9 08-14 21:01:26.469 4312-4312/? I/Process: Sending signal. PID: 4312 SIG: 9

You are not printing NPE, this is why you do not have it in the log. 您没有打印NPE,这就是为什么在日志中没有它的原因。

You need to use Fragments instead of setting new content view. 您需要使用片段而不是设置新的内容视图。

You would never need System.exit(0) in Android beacuse it is against the system, you have to use finish from your Activity class. 您永远不需要Android中的System.exit(0),因为它与系统相对,您必须使用Activity类中的finish

Instead of using checking button, you can set onClickListener into your RadioGroup and you will get events every time selection has been made, so you can proceed to the next Screen (Activity or Fragment, but threre is no reason to use another one setContentView). 您可以将onClickListener设置为RadioGroup而不是使用检查按钮,并且每次选择时都会获取事件,因此可以继续下一个Screen(Activity或Fragment,但是没有理由使用另一个setContentView)。

Here is good explanation about setContentView: Android: is using setContentView multiple times bad while changing layouts? 这是有关setContentView的很好的解释: Android:更改布局时多次使用setContentView是不好的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM