java - 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");
}
}
}
解决方案
使用表达式PageNo==1
进行比较。UsingPageNo=1
将返回 1,它的类型为int
。while
表达式需要一个布尔条件来评估。因此,使用PageNo==1
which 是相等运算符并返回true
orfalse
类型boolean
。
推荐阅读
- r - 恢复运算符的语法糖
- c# - 使用 Linq 的字符串长度数组
- jquery - 单击 whitout 控件时,引导轮播在 ios 上滑动
- javascript - 如何将这个动态添加的复选框的值放入隐藏的内容中?
- java - 如何从浏览器使用@PostMapping?
- c# - 通过 C# 加密的 T-SQL 数据解密
- ubuntu - 每 3 小时 cronjob 和每 3+1 小时 cronjob
- javascript - 隐藏/禁用特定图形值的提示音
- javascript - svg.js - 如何从 SVG 文本元素中获取 X 位置
- tensorflow - tensorflow:写入失败,因为无法打开文件