简体   繁体   中英

Application stops when i change everything in a button/textview except the Text

im using 2 Android Books and android-studio to get into the materia but now im stuck. If i try to change .clickable(false), .setenabled(false) or even the color in Java, my programm stops. It runs fluent with every gimmick im implementing but if i try to enable (first state disabled) the checkboxes/buttons, when someone clicks on the "startbutton", my app closes.

Note about the ammount of Textviews and checkboxes: Im about to check out the mainfunctions and make it in the 1.0 in a scrollable activity with an objectarray. atm all these textviews are only there to get me a hint how big this app can be. It´sa quizapp for exams about health and medicine. It is possible that there is 1 question 10 subquestions and 10 answers or 1 questions, 0 subquestions and only 2 answers. Every Exam has about 60 Questions. BUT i can´t do this cause when i disable one single checkbox or setVisebility(false) my App stops.

My suggestions:

  • to open a "getCurentState()" method in the xml File
  • create a new xml file with one "true" and a "false" varable and ref them in the main xml document setclickable="@.../currentState"
  • ivé heard about xsl but how? :D

XML-File:

<TextView
    android:id="@+id/examchoosen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.983"
    app:layout_constraintHorizontal_bias="0.92" />

<Button
    android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="false"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintVertical_bias="0.982" />

<TextView
    android:id="@+id/countdown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.016"
    app:layout_constraintHorizontal_bias="0.501" />

<TextView
    android:id="@+id/frage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="42dp"
    app:layout_constraintHorizontal_bias="0.065"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="0.022" />

<TextView
    android:id="@+id/subfrage1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteY="84dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.103" />

<TextView
    android:id="@+id/subfrage2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteY="101dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.103" />

<TextView
    android:id="@+id/subfrage3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="118dp"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/subfrage4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteY="135dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.103" />

<TextView
    android:id="@+id/subfrage5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="152dp" />

<TextView
    android:id="@+id/subfrage6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="169dp" />

<TextView
    android:id="@+id/subfrage7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="186dp" />

<TextView
    android:id="@+id/subfrage8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="203dp" />

<TextView
    android:id="@+id/subfrage9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteY="220dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.103" />

<TextView
    android:id="@+id/subfrage10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="237dp" />

<TextView
    android:id="@+id/AntwortA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="273dp" />

<TextView
    android:id="@+id/AntwortB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="290dp" />

<TextView
    android:id="@+id/AntwortC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="307dp" />

<TextView
    android:id="@+id/AntwortD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="325dp" />

<TextView
    android:id="@+id/AntwortE"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="342dp" />

<TextView
    android:id="@+id/AntwortF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="359dp" />

<TextView
    android:id="@+id/AntwortG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="376dp" />

<TextView
    android:id="@+id/AntwortH"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="393dp" />

<TextView
    android:id="@+id/AntwortI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="410dp" />

<TextView
    android:id="@+id/AntwortJ"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="427dp" />

<CheckBox
    android:id="@+id/BoxA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.113"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="206dp" />

<CheckBox
    android:id="@+id/BoxB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="189dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.023"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<CheckBox
    android:id="@+id/BoxC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="172dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.113"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<CheckBox
    android:id="@+id/BoxD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="155dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.023"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<CheckBox
    android:id="@+id/BoxE"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="137dp"
    app:layout_constraintHorizontal_bias="0.113" />

<CheckBox
    android:id="@+id/BoxF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="120dp"
    app:layout_constraintHorizontal_bias="0.023" />

<CheckBox
    android:id="@+id/BoxG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="103dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.113"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<CheckBox
    android:id="@+id/BoxH"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="86dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.023"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<CheckBox
    android:id="@+id/BoxI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="69dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.113"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<CheckBox
    android:id="@+id/BoxJ"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="52dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.023"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

Java File:

package de.isolde_richter.hp_pruefer_00;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class examenExaminer extends AppCompatActivity {




TextView frage;
TextView subfrage1;
TextView subfrage2;
TextView subfrage3;
TextView subfrage4;
TextView subfrage5;
TextView subfrage6;
TextView subfrage7;
TextView subfrage8;
TextView subfrage9;
TextView subfrage10;
TextView antwortA;
TextView antwortB;
TextView antwortC;
TextView antwortD;
TextView antwortE;
TextView antwortF;
TextView antwortG;
TextView antwortH;
TextView antwortI;
TextView antwortJ;
CheckBox BoxA;
CheckBox BoxB;
CheckBox BoxC;
CheckBox BoxD;
CheckBox BoxE;
CheckBox BoxF;
CheckBox BoxG;
CheckBox BoxH;
CheckBox BoxI;
CheckBox BoxJ;
TextView examchoosen;
Button counter;
TextView countdown;
CountDownTimer counterTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    frage = (TextView) findViewById(R.id.frage);
    subfrage1 = (TextView) findViewById(R.id.subfrage1);
    subfrage2 = (TextView) findViewById(R.id.subfrage2);
    subfrage3 = (TextView) findViewById(R.id.subfrage3);
    subfrage4 = (TextView) findViewById(R.id.subfrage4);
    subfrage5 = (TextView) findViewById(R.id.subfrage5);
    subfrage6 = (TextView) findViewById(R.id.subfrage6);
    subfrage7 = (TextView) findViewById(R.id.subfrage7);
    subfrage8 = (TextView) findViewById(R.id.subfrage8);
    subfrage9 = (TextView) findViewById(R.id.subfrage9);
    subfrage10 = (TextView) findViewById(R.id.subfrage10);
    antwortA=(TextView) findViewById(R.id.AntwortA);
    antwortB=(TextView) findViewById(R.id.AntwortB);
    antwortC=(TextView) findViewById(R.id.AntwortC);
    antwortD=(TextView) findViewById(R.id.AntwortD);
    antwortE=(TextView) findViewById(R.id.AntwortE);
    antwortF=(TextView) findViewById(R.id.AntwortF);
    antwortG=(TextView) findViewById(R.id.AntwortG);
    antwortH=(TextView) findViewById(R.id.AntwortH);
    antwortI=(TextView) findViewById(R.id.AntwortI);
    antwortJ=(TextView) findViewById(R.id.AntwortJ);
    BoxA =(CheckBox)findViewById(R.id.BoxA);
    BoxB =(CheckBox)findViewById(R.id.BoxB);
    BoxC =(CheckBox)findViewById(R.id.BoxC);
    BoxD =(CheckBox)findViewById(R.id.BoxD);
    BoxE =(CheckBox)findViewById(R.id.BoxE);
    BoxF =(CheckBox)findViewById(R.id.BoxF);
    BoxG =(CheckBox)findViewById(R.id.BoxG);
    BoxH =(CheckBox)findViewById(R.id.BoxH);
    BoxI =(CheckBox)findViewById(R.id.BoxI);
    BoxJ =(CheckBox)findViewById(R.id.BoxJ);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_examen_examiner);
    examchoosen = (TextView) findViewById(R.id.examchoosen);
    counter = (Button) findViewById(R.id.counter);
    countdown = (TextView) findViewById(R.id.countdown);
    countdown.setText("(120M:00S)");
    counter.setText(R.string.rdy);

    counterTimer = new CountDownTimer(7200000,100) {
        @Override
        public void onTick(long l) {

            Long totals =(l/100)/10;
            Long m = totals/60;
            Long s = totals%60;
            //countdown.setText(""+m+"M:"+s+"S");



            if(m>=10 && s >=10){
                countdown.setText("("+m+"M:"+s+"S)"); //bsp 1 = (10M:10S)
            }  else if (m < 10 && s >=10) {
                countdown.setText("(0"+m+"M:"+s+"S)");  //bsp 2 =    (09M:10S)
            } else if (m < 10  && s < 10) {
                countdown.setText("(0"+ m+"M:0"+s+"S)"); //bsp 3 (09M:09S)
            } else if (m == 0 && s<10){
                countdown.setText("(00M:0"+s+"S)");//bsp 5 (00M:09S)
            }  else if (m >= 10 && s < 10){
                countdown.setText("("+m+"M:0"+s+"S)"); //bsp 1 = (10M:09S)
            }
        }

        @Override
        public void onFinish() {
            countdown.setText("(00M:00S)");
            counter.setText("Fertig");
        }
    };

    counter.setOnClickListener(new View.OnClickListener(){
        private boolean startExam=false;
        @Override
        public void onClick(View w){
            //wenn der button geklickt wird
            //if the button gets clicked
            if(startExam==false) {
                counter.setText(R.string.next); // <- Works
                counterTimer.start();
                startExam = true;
            }
        }
    });


}
}

If someone encounters this problem this should help you, in my case it worked. instead of

  listview.setVisibility(true);

do this:

  listview.setVisibility(View.VISIBLE); 

same thing for Buttons,Textviews or Checkboxes.

  checkBox.setChecked(true); //that won´t work if you add dynamically in a listview or linearlayout
  checkBox.toggle(); // will do the job, maybe it will work with .setChecked(!ischecked); but i had more problems with it 

As far as i have worked with android-studio, i recognised that if you use true/false (boolean) or a fixed number (even if the settings are 1 and 0) for colors in coding, your app will have many problems and won´t run fluent. In an activity you need to speak to the Parentclass (View) and select the variable viá dot. View.VISIBLE (which is an int) View.INVISIBLE (which is also an int)

Hope this will help someone, cause i didn´t got an propper solution for my problem. Android requiers an int instead of a boolean, even if the constructor want´sa boolean. Crazy stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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