【问题标题】:1.button not displaying text 2. is logic correct1.按钮不显示文字 2.逻辑正确
【发布时间】:2014-09-21 18:24:08
【问题描述】:

问题 1)由于某种原因,当我单击“生成”时,它不显示任何文本。我希望按钮执行以下操作:在用户输入 3 个值(int)后,例如:1、1、2 和单击“生成”按钮,显示消息应为“等腰三角形:2 个全等边”——这在以前有效,但我的逻辑错误,我在这里问了一个问题:If statement not working for android programming。然后我尝试实施这些建议,现在它有一个不同的错误

问题2)我的逻辑正确吗?解决此问题后,我能否显示正确的结果?

请帮忙,我是 android 编程新手。提前非常感谢。

java代码:

package com.example.trianglegame;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TriangleGame extends Activity {// implement-have to use all of the
// methods
// set up the variables here

Button Gen;
EditText Input1;
EditText Input2;
EditText Input3;
String input1;
String input2;
String input3;

TextView Display;

int a;
int b;
int c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    // assigning the values
    Input1 = (EditText) findViewById(R.id.editText1);
    Input2 = (EditText) findViewById(R.id.editText2);
    Input3 = (EditText) findViewById(R.id.editText3);

    Display = (TextView) findViewById(R.id.textView5);

    Gen = (Button) findViewById(R.id.button1);

    Gen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // getting the text and converting it to string value
            input1 = Input1.getText().toString();
            input2 = Input2.getText().toString();
            input3 = Input2.getText().toString();

            // converting those text values back into int
            a = Integer.parseInt(input1);
            b = Integer.parseInt(input2);
            c = Integer.parseInt(input3);

            // displaying the message

            if ((a == b && b != c) || (a == c && b != c)
                    || (b == c && a != c)) {
                Display.setText("Isosceles Triangle: 2 Congruent Sides");
            } else if (a == b && a == c) {
                Display.setText("Equilateral Triangle:All sides are equal");
            }

            else if (a != b && a != c && b != c) {
                Display.setText("Scalene Triangle: No Congruent Sides");
            } else {
                Display.setText("Error");
            }

        }
    });
}
}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/enter_text" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/side_1" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/side_2" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/type_hint"
    android:text="@string/side_3" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/generate" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear" />
</LinearLayout>

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

LogCat 错误

09-21 18:50:20.739: E/Zygote(32): setreuid() failed. errno: 2
09-21 18:50:24.939: E/Zygote(32): setreuid() failed. errno: 17
09-21 18:50:25.579: E/BatteryService(58): usbOnlinePath not found
09-21 18:50:25.579: E/BatteryService(58): batteryVoltagePath not found
09-21 18:50:25.579: E/BatteryService(58): batteryTemperaturePath not found
09-21 18:50:25.589: E/SurfaceFlinger(58): Couldn't open /sys/power/wait_for_fb_sleep or  /sys/power/wait_for_fb_wake
09-21 18:50:28.279: E/EventHub(58): could not get driver version for /dev/input/mouse0, Not a typewriter
09-21 18:50:28.279: E/EventHub(58): could not get driver version for /dev/input/mice, Not a typewriter
09-21 18:50:28.379: E/System(58): Failure starting core service
09-21 18:50:28.379: E/System(58): java.lang.SecurityException
09-21 18:50:28.379: E/System(58):   at android.os.BinderProxy.transact(Native Method)
09-21 18:50:28.379: E/System(58):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
09-21 18:50:28.379: E/System(58):   at android.os.ServiceManager.addService(ServiceManager.java:72)
09-21 18:50:28.379: E/System(58):   at com.android.server.ServerThread.run(SystemServer.java:184)
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/Effect_Tick.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressStandard.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressDelete.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressReturn.ogg
09-21 18:50:29.359: E/ThrottleService(58): Could not open GPS configuration file /etc/gps.conf
09-21 18:50:29.819: E/logwrapper(133): executing /system/bin/tc failed: No such file or directory
09-21 18:50:29.889: E/logwrapper(135): executing /system/bin/tc failed: No such file or directory
09-21 18:50:29.889: E/logwrapper(136): executing /system/bin/tc failed: No such file or directory
09-21 18:50:32.957: E/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3

【问题讨论】:

  • 你想在按钮点击时做什么..请指定。
  • 在 Java 和 Android 中,您的对象和变量名称必须以小写开头,例如 Button gen;,除非它们是 public static final
  • 嗨,我将所有输入更改为小写,但单击生成按钮后仍然不显示文本
  • 你的 logcat 显示了什么?在按钮点击中使用 system.out.println() 来检测按钮点击是否被触发
  • 这是模拟器的路径。我对其进行了调整,一切正常。主要问题是我的按钮覆盖了我的文本,所以代码和逻辑都很好。感谢您的帮助。

标签: java android xml button if-statement


【解决方案1】:

代替:

input2 = Input2.getText().toString();
input3 = Input2.getText().toString();

使用:

input2 = Input2.getText().toString();
input3 = Input3.getText().toString();

【讨论】:

  • 感谢您注意到这一点。我试过了,但我仍然得到同样的错误,所以它一定是那个和别的东西。
  • 1,1,2的输出是什么?
  • 起初它没有显示任何东西,这是因为我的按钮覆盖了它:) 我现在知道,除了你给我看的错字之外,一切都很好。再次感谢
  • 很高兴知道,因为我一直盯着你的代码不知道出了什么问题:D
  • 哈哈,我是这个网站的新手,所以我问的问题,我想其他人已经问过了,出于某种原因,我的声望点下降了……希望我以后还能问问题。但是,是的,我总是研究问题,通常找不到答案,这就是我发布它的原因:)
【解决方案2】:

据我所见,逻辑很好,虽然我会以不同的方式检查,更具可读性。

int n = 0; //number of equal sides

if(a==b)
  n++;
if(a==c)
  n++;
if(b==c)
  n++;

//Display seems like a class, but is a variable, should start with lower case and have camel notation such as displayTextView
switch(n) {
  case 0:
    Display.setText("Scalen");
    break;
  case 1:
    Display.setText("Isosceles");
    break;
  case 2: case 3:
    Display.setText("Equilateral");
    break;
}

至于它显示的错误,您可以发布日志吗?

【讨论】:

  • 嗨,当我运行程序时,我在 LogCat 中没有看到任何错误,所以我无法发布它:(
  • 当应用程序崩溃时,如果您已连接手机,LogCat 应该打印一些内容,请尝试连接 IDE
  • 您好,感谢您的回答。是的,它现在打印了一些错误信息。我将编辑我的帖子
  • 我想这不是我们要找的日志哈哈,你有异常描述吗?为了清楚起见,您的 xml 是 game.xml?
  • 是的,它是 game.xml 。嗨,我没有为输入值抛出异常。我应该这样做吗?您是否建议我查看如何为这些输入值抛出异常?
猜你喜欢
  • 2021-05-28
  • 2014-04-08
  • 2013-08-22
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
相关资源
最近更新 更多