【问题标题】:Checkbuttons crashes app when checkedCheckbuttons 选中时会导致应用程序崩溃
【发布时间】:2017-03-18 10:29:00
【问题描述】:

我创建了一个复选框列表,并尝试根据所选复选框的数量按比例增加一个 int 值。相反,每次我尝试检查它们(通过 .isChecked())时,应用程序都会崩溃。我试图到处搜索一些解决方案,但没有任何效果。我真的不明白问题出在哪里。

这是代码

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.deadlybanana.myfirstapp.DisplayPicture">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp">
        <LinearLayout
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/unicorn" />

        <TextView
            android:id="@+id/frasenome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="" />

        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Are you Beatiful?" />

        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Are you Fabolous?" />

        <CheckBox
            android:id="@+id/checkbox3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Are you Unique?" />

        <CheckBox
            android:id="@+id/checkbox4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Are you a horse?" />

        <CheckBox
            android:id="@+id/checkbox5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Do you eat grass?" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="checkbutton"
            android:text="Check if you are a unicorn" />
        </LinearLayout>


    </ScrollView>

</android.support.constraint.ConstraintLayout>

Java 文件

package com.example.deadlybanana.myfirstapp;
public class DisplayPicture extends AppCompatActivity {

List<Boolean> checkboxes = new ArrayList<Boolean>();
CheckBox press1;
CheckBox press2;
CheckBox press3;
CheckBox press4;
CheckBox press5;
int points = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_picture);
    Intent intent = getIntent();

    String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);
    CheckBox press2 = (CheckBox)findViewById(R.id.checkbox2);
    CheckBox press3 = (CheckBox)findViewById(R.id.checkbox3);
    CheckBox press4 = (CheckBox)findViewById(R.id.checkbox4);
    CheckBox press5 = (CheckBox)findViewById(R.id.checkbox5);

    TextView NameString = (TextView) findViewById(R.id.frasenome);
    String phrase = name + ", are you some of these things?";
    NameString.setText(phrase);
}

public void checkbutton(View view){
    if (press1.isChecked()){
        points += 4;
    }
    if (points > 3){
        Toast.makeText(this, "You are a unicorn", Toast.LENGTH_LONG).show();
    } else{
        Toast.makeText(this, "You are not a unicorn",   Toast.LENGTH_LONG).show();
    }
    }
}

(现在只检查第一个复选框以简化代码)

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    试试这个:onCreate()

    press1 = (CheckBox)findViewById(R.id.checkbox1);
    press2 = (CheckBox)findViewById(R.id.checkbox2);
    press3 = (CheckBox)findViewById(R.id.checkbox3);
    press4 = (CheckBox)findViewById(R.id.checkbox4);
    press5 = (CheckBox)findViewById(R.id.checkbox5);
    

    press1.isChecked() 引用全局 CheckBox press1; 时发生错误

    尚未初始化..

    您正在初始化复选框,例如:

    CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);onCreate() 内,它只给它一个本地实例,在你的 checkbutton(View view) 函数中是不可访问的

    解决方案

    去掉本地初始化就用:press1 = (CheckBox)findViewById(R.id.checkbox1);

    【讨论】:

      【解决方案2】:

      为您的按钮设置 id 假设它是 button 并添加以下代码

      Button btn = (Button)findViewById(R.id.button);
       btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  checkbutton();
              }
          });
      

      注意:去掉视图参数

      实现监听器是处理事件的最佳方式

      【讨论】:

        【解决方案3】:

        我从不使用 isChecked() 方法,因为它对我不起作用。您想在选择复选按钮时增加点数并在按下按钮时显示点数吗?

        这是课程代码:

        public class ButtonActivity extends AppCompatActivity
        {
        int points=0;
        boolean check1=false;
        boolean check2=false;
        boolean check3=false;
        boolean check4=false;
        boolean check5=false;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button);
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Toast.makeText(getApplicationContext(),points+"",Toast.LENGTH_SHORT).show();
                }
            });
        }
        
        public void onClick(View view)
        {
            switch(view.getId())
            {
                case R.id.checkBox:
                    if(check1)
                    {
                        check1=false;
                        points--;
                    }
                    else
                    {
                        check1=true;
                        points++;
                    }
                    break;
                case R.id.checkBox2:
                    if(check2)
                    {
                        check2=false;
                        points--;
                    }
                    else
                    {
                        check2=true;
                        points++;
                    }
                    break;
                case R.id.checkBox3:
                    if(check3)
                    {
                        check3=false;
                        points--;
                    }
                    else
                    {
                        check3=true;
                        points++;
                    }
                    break;
                case R.id.checkBox4:
                    if(check4)
                    {
                        check4=false;
                        points--;
                    }
                    else
                    {
                        check4=true;
                        points++;
                    }
                    break;
                case R.id.checkBox5:
                    if(check5)
                    {
                        check5=false;
                        points--;
                    }
                    else
                    {
                        check5=true;
                        points++;
                    }
                    break;
            }
        }
        }
        

        这是布局代码:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context="com.example.utente.stackoverflow.ButtonActivity">
        
        <CheckBox
            android:text="CheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox"
            android:onClick="onClick"/>
        
        <CheckBox
            android:text="CheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox2"
            android:onClick="onClick"/>
        
        <CheckBox
            android:text="CheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox3"
            android:onClick="onClick"/>
        
        <CheckBox
            android:text="CheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox4"
            android:onClick="onClick"/>
        
        <CheckBox
            android:text="CheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox5"
            android:onClick="onClick"/>
        
        <Button
            android:text="Points"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button" />
        

        我测试了它并且它有效。希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-09-21
          • 1970-01-01
          • 2015-08-13
          • 2020-06-29
          • 1970-01-01
          • 2015-10-21
          • 2016-06-04
          相关资源
          最近更新 更多