首页 > 解决方案 > While loop requires boolean but found int

问题描述

I have been stuck on this for ages. On the click of a button in Android Studio I want the value of an integer (called PageNo) to increase by 1. I want different text to appear in the TextView box (Act2PageNo) depending on the value. Eg if PageNo=2 then Act2PageNo will give a description of Option 2 of a particular product. However, it is not recognising my code in the While loop saying that my 'while (PageNo=1)' section has provided an integer but requires a Boolean. Also mentions incompatible types.

My code is below: All help appreciated.

package com.example.wma76143.testappactivitychange;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import javax.xml.transform.Result;

public class Activity2 extends AppCompatActivity {
private TextView Act2Title;
Button btnnNext;
Button btnPrevious;
private TextView Act2PageNo;
private int PageNo = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        Act2Title = (TextView) findViewById(R.id.textView);
        Act2PageNo = (TextView) findViewById(R.id.textPageNo);
        btnnNext = (Button) findViewById(R.id.buttonNext);
        btnPrevious = (Button) findViewById(R.id.buttonPrevious);


        btnnNext.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                PageNo++;
                Gotonextpage();}

        });
    }
    public void Gotonextpage() {
        while (PageNo=1){
           Act2PageNo.setText("Text will be put here to describe Option 1");
        }
        while (PageNo=2){
            Act2PageNo.setText("Text will be put here to describe Option 2");
        }
        while (PageNo=3){
            Act2PageNo.setText("Text will be put here to describe Option 3");
        }

    }

    }

标签: javaandroidincompatibletypeerror

解决方案


使用表达式PageNo==1进行比较。UsingPageNo=1将返回 1,它的类型为intwhile表达式需要一个布尔条件来评估。因此,使用PageNo==1which 是相等运算符并返回trueorfalse类型boolean


推荐阅读