逻辑错误; TextView中甚至设置为空后返回 [英] Logic Error; TextView returns even after setting to null
问题描述
我有一个简单的Android应用程序中的逻辑错误;
我有3个按钮
(置换,清除和GC),1 的EditText
和的TextView
。当点击置换按钮,它发出了的EditText
框中的文本从其他类中的方法,并将返回值的的TextView
。返回的值就是乱码的文字。
当我点击的清除的,它清除的EditText
框和的TextView $的当前内容C $ C>; (由内容设置为空字符串)。
然而,当我点击置换与在文本框中新值,旧的(清除)文本以及返回与下面的较新的文本
这里的code:
包com.mobinga.string;
进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.os.Vibrator;
进口android.util.Log;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口com.mob.string.Perm;
公共类MoPermActivity扩展活动实现View.OnClickListener {
按钮BTN;
按钮BTN2;
按钮btn3;
的EditText等;
TextView的电视;
振动器六;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
BTN =(按钮)findViewById(R.id.Permute);
BTN2 =(按钮)findViewById(R.id.Clear);
btn3 =(按钮)findViewById(R.id.GC);
btn.setOnClickListener(本);
btn2.setOnClickListener(本);
btn3.setOnClickListener(本);
等=(EditText上)findViewById(R.id.Text);
电视=(TextView中)findViewById(R.id.textView1);
}
公共无效的onClick(视图查看){
开关(view.getId()){
案例R.id.Permute:
VI =(震动)getSystemService(Context.VIBRATOR_SERVICE);
vi.vibrate(50);
tv.setText(Perm.perm(et.getText()的toString())。);
打破;
案例R.id.Clear:
VI =(震动)getSystemService(Context.VIBRATOR_SERVICE);
et.setText();
tv.setText();
vi.vibrate(50);
打破;
案例R.id.GC:
VI =(震动)getSystemService(Context.VIBRATOR_SERVICE);
GC();
vi.vibrate(50);
}
}
公共静态无效的GC(){
Log.d(置换,呼唤气相色谱法);
System.gc()的;
}
}
我如何正确和完全清除的TextView的内容,并从返回停止吗?简单地说:我的问题是,TextView中的所谓清理内容连清后返回
修改如果有必要,这里的烫发类
包com.mob.string;
公共类烫发{
静态字符串V =;
静态无效的置换(INT水平,串置换,布尔用[],字符串原件){
INT长度= original.length();
如果(水平==长度){
V + =置换+\ N的;
} 其他 {
的for(int i = 0; I<长度;我++){
如果(!使用[I]){
使用[我] = TRUE;
置换(级别+ 1,置换+ original.charAt(i)中,使用时,原);
使用[我] = FALSE;
}
}
}
}
公共字符串烫发(String s)将{
布尔使用[] = {假的,假的,假的,假的,假的,假的,假};
置换(0,,使用,多个);
返回伏;
}
}
v已不会被重置。
公共字符串烫发(String s)将{
ν=;
布尔使用[] = {假的,假的,假的,假的,假的,假的,假};
置换(0,,使用,多个);
返回伏;
}
不过,我不明白为什么V,即使存在,是诚实的。我将通过递归调用过去了吧。
I have a logic error in a simple android app;
I have 3 Buttons
(Permute, Clear and GC), 1 EditText
and a TextView
. When the Permute button is clicked it sends the text in the EditText
box to a method from another class and sets the returned value to the TextView
. The returned value is just gibberish text.
When I click the Clear, it clears the current contents of the EditText
box and the TextView
; (by setting the contents to a null string "").
However when I click Permute with new values in the textBox, the old (cleared) text returns along with the newer text below
Here's the code:
package com.mobinga.string;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.mob.string.Perm;
public class MoPermActivity extends Activity implements View.OnClickListener {
Button btn;
Button btn2;
Button btn3;
EditText et;
TextView tv;
Vibrator vi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Permute);
btn2 = (Button) findViewById(R.id.Clear);
btn3 = (Button) findViewById(R.id.GC);
btn.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
et = (EditText) findViewById(R.id.Text);
tv = (TextView) findViewById(R.id.textView1);
}
public void onClick(View view){
switch(view.getId()){
case R.id.Permute:
vi = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vi.vibrate(50);
tv.setText(Perm.perm(et.getText().toString()));
break;
case R.id.Clear:
vi = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
et.setText("");
tv.setText("");
vi.vibrate(50);
break;
case R.id.GC:
vi = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
GC();
vi.vibrate(50);
}
}
public static void GC(){
Log.d("Permute","Calling GC");
System.gc();
}
}
How do I properly and completely clear the contents of TextView, and stop it from returning? Simply put : My problem is that the supposedly cleared contents of the TextView return back even after clearing.
Edit if necessary here's the Perm Class
package com.mob.string;
public class Perm {
static String v = "";
static void permute(int level, String permuted, boolean used[], String original) {
int length = original.length();
if (level == length) {
v +=permuted+"\n";
} else {
for (int i = 0; i < length; i++) {
if (!used[i]) {
used[i] = true;
permute(level + 1, permuted + original.charAt(i), used, original);
used[i] = false;
}
}
}
}
public String perm(String s){
boolean used[] = {false, false, false, false, false,false,false};
permute(0, "", used, s);
return v;
}
}
v is never reset.
public String perm(String s){
v = "";
boolean used[] = {false, false, false, false, false,false,false};
permute(0, "", used, s);
return v;
}
However I don't see why v even exists to be honest. I would of passed it through the recursive calls.
这篇关于逻辑错误; TextView中甚至设置为空后返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!