【问题标题】:Error with the if and else statementif 和 else 语句出错
【发布时间】:2016-03-13 20:10:04
【问题描述】:

我收到 3 个不同的错误:预期标识符、意外令牌、未知类:'score'。这些错误在第 57-69 行。

此代码的重点是检查是否检查了清单,如果是,则将分数加 1。它会根据分数将输出文本更改为不同的字符串。

package xyz.ashraf.whoisdelasalle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * Created by Ashraf on 3/2/2016.
 */
public class check_Button extends Pop_sallian{
    // Connects The variable to an xml id


    TextView output = (TextView) findViewById(R.id.output);

    //sets the variable to 0
    int score = 0;

    public void onCheckboxClicked(View view) {
        boolean checked = ((CheckBox) view).isChecked();

        switch(view.getId()){
            case R.id.concern:
                if(checked) {
                    score += 1;
                }
                break;
            case R.id.faith:
                if(checked){
                    score+=1;
                }
                break;
            case R.id.respect:
                if(checked){
                    score+=1;
                }
                break;
            case R.id.education:
                if(checked){
                    score+=1;
                }
                break;
            case R.id.community:
                if(checked){
                    score+=1;
                }
                break;
        }
    }
    // adds the variables together to form a score

    if(score == 0){
        output.setText("Come on! Get involved, your la sallian community needs you.");
    } else if( score == 1){
        output.setText("Good start, keep going!");
    } else if( score == 2){
        output.setText("Room to improve but doing good!");
    } else if(score == 3){
        output.setText("Very good, others look up to you!");
    } else if(score == 4){
        output.setText("Wow, you really are an inspiration");
    } else if(score == 5){
        output.setText("Excellent! You're a leader in your la sallian community");
    } else{
        output.setText("Unknown");
    }
    // changes the output text based on score value
}

^^ 错误所在的代码^^

package xyz.ashraf.whoisdelasalle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;

/**
 * Created by Ashraf on 1/27/2016.
 */
public class Pop_sallian extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.popwindow_sallian);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width*.8),(int)(height*.6));

        Button checkButton = (Button) findViewById(R.id.check);
        checkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Pop_sallian.this, check_Button.class));
            }
        });
        Button okButton = (Button) findViewById(R.id.okButton_sallian);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

^^ 弹出屏幕的启动和检查按钮所在的代码^^

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<RelativeLayout
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Are you a Sallian?"
        android:id="@+id/textView7"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:textColor="#000000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
        android:id="@+id/textView8"
        android:layout_below="@+id/textView7"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:textColor="#000000" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Are you concerened for the poor and Social Justice?"
        android:id="@+id/concern"
        android:textSize="18sp"
        android:layout_below="@+id/textView8"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:onClick="onCheckboxClicked"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you have faith in the presence of God?"
        android:id="@+id/faith"
        android:textSize="15sp"
        android:layout_below="@+id/concern"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="3dp"
        android:onClick="onCheckboxClicked"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you have Respect for all people?"
        android:id="@+id/respect"
        android:layout_below="@+id/faith"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="3dp"
        android:textSize="15sp"
        android:onClick="onCheckboxClicked"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you value education?"
        android:id="@+id/education"
        android:textSize="15sp"
        android:layout_below="@+id/respect"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="3dp"
        android:onClick="onCheckboxClicked"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Are you inclusive in your community?"
        android:id="@+id/community"
        android:layout_below="@+id/education"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="15sp"
        android:checked="false"
        android:onClick="onCheckboxClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:id="@+id/okButton_sallian"
        android:layout_below="@+id/community"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="20dp"
        android:layout_marginTop="90dp"
        android:layout_marginBottom="20dp"
        android:background="#FAFAFA"
        android:textColor="#00E676"
        android:elevation="2dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check"
        android:id="@+id/check"
        android:textColor="#00E676"
        android:elevation="2dp"
        android:background="#FAFAFA"
        android:layout_alignTop="@+id/okButton_sallian"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/output"
        android:textColor="#1eff00"
        android:textSize="20sp"
        android:layout_below="@+id/community"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/check"
        android:textIsSelectable="false" />
</RelativeLayout>
    </ScrollView>

^^XML 代码^^

【问题讨论】:

  • 为什么不用 switch 语句来代替几个 if 语句?
  • 这样可以修复错误吗?
  • 在下面查看我的答案。这是一个错位的大括号。
  • 如果你想要有人帮助你,我个人会更倾向于如果你有一个 git url 我可以用来拉下这段代码并查看一下。
  • 当您将受影响的部分拉出时可能会发生的情况是您将隔离问题并可能自己解决它,双赢

标签: android


【解决方案1】:

您的大括号 } 放错了位置。该函数在if 之前结束。

public void onCheckboxClicked(View view) {
        boolean checked = ((CheckBox) view).isChecked();

        switch(view.getId()){
            case R.id.concern:
                if(checked) {
                    score += 1;
                }
                break;
            case R.id.faith:
                if(checked){
                    score+=1;
                }
                break;
            case R.id.respect:
                if(checked){
                    score+=1;
                }
                break;
            case R.id.education:
                if(checked){
                    score+=1;
                }
                break;
            case R.id.community:
                if(checked){
                    score+=1;
                }
                break;
        }
        // adds the variables together to form a score

        //} <-- REMOVE THIS CURLY BRACKET

        if(score == 0){
            output.setText("Come on! Get involved, your la sallian community needs you.");
        } else if( score == 1){
            output.setText("Good start, keep going!");
        } else if( score == 2){
            output.setText("Room to improve but doing good!");
        } else if(score == 3){
            output.setText("Very good, others look up to you!");
        } else if(score == 4){
            output.setText("Wow, you really are an inspiration");
        } else if(score == 5){
            output.setText("Excellent! You're a leader in your la sallian community");
        } else{
            output.setText("Unknown");
        }
        // changes the output text based on score value
 } // <-- MOVE THE CURLY BRACKET HERE

【讨论】:

  • 它可以工作,但有一个新错误。当我使用手机单击复选框时,应用程序停止并出现错误:java.lang.IllegalStateException:在视图类 android.widget 上定义的 android:onClick 属性的父或祖先上下文中找不到方法 onCheckboxClicked(View)。 id 为“尊重”的复选框
猜你喜欢
  • 1970-01-01
  • 2016-07-21
  • 2015-01-05
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2014-02-15
相关资源
最近更新 更多