2014-10-19 43 views
0

我有一个简单的高尔夫记分卡应用程序,我正在开发。现在我有9个文字浏览,您可以逐个输入分数。当输入分数时,如果低于标准,TextView背景变为绿色,如果是上面的则为红色,如果是平坦的,则为白色。 IU已经使用TextWatchers完成了这项工作。因为我添加了TextWatcher,无法清除TextView?

我有一个清楚的所有字段类,它清除所有始终工作的文字浏览。但是因为我添加了textwatchers的分数,所以我清除了所有的textViews已经停止工作。该程序崩溃。

这是我清楚所有的TextView代码,

公共类clearButtonListner实现OnClickListener {

private Activity activity; 

public clearButtonListner(Activity activity){ 

    this.activity = activity; 
} 

@Override 
public void onClick(View v) { 


    TextView et1 = (TextView)this.activity.findViewById(R.id.EditTextScore1); 
    TextView et2 = (TextView)this.activity.findViewById(R.id.EditTextScore2); 
    TextView et3 = (TextView)this.activity.findViewById(R.id.EditTextScore3); 
    TextView et4 = (TextView)this.activity.findViewById(R.id.EditTextScore4); 
    TextView et5 = (TextView)this.activity.findViewById(R.id.EditTextScore5); 
    TextView et6 = (TextView)this.activity.findViewById(R.id.EditTextScore6); 
    TextView et7 = (TextView)this.activity.findViewById(R.id.EditTextScore7); 
    TextView et8 = (TextView)this.activity.findViewById(R.id.EditTextScore8); 
    TextView et9 = (TextView)this.activity.findViewById(R.id.EditTextScore9); 
    TextView ettotal = (TextView)this.activity.findViewById(R.id.editTextTotalScore); 


    et1.setText(""); 
    et2.setText(""); 
    et3.setText(""); 
    et4.setText(""); 
    et5.setText(""); 
    et6.setText(""); 
    et7.setText(""); 
    et8.setText(""); 
    et9.setText(""); 
    ettotal.setText(""); 

} 

}

我textWatchers范例之一, 公共类textWatcher1实现TextWatcher {

private Activity activity; 

public textWatcher1(Activity activity){ 

    this.activity = activity; 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void afterTextChanged(Editable s) { 



    int p1 = 5; 
    // Create my TextViews 
    TextView et1 = (TextView)this.activity.findViewById(R.id.EditTextScore1); 

    int s1 = (int)(Integer.parseInt(et1.getText().toString())); 

    if(s1 < p1){ 

    et1.setBackgroundColor(Color.GREEN);  

    } 


    else if(s1 > p1){ 

    et1.setBackgroundColor(Color.RED);  

    } 

    else if(s1 == p1){ 

    et1.setBackgroundColor(Color.WHITE);   

    } 
} 

}

任何帮助将是伟大的。该应用程序崩溃与抛出新的NumberFormatException(“无效int:\”“+ s +”\“”);例外。

+0

该应用程序崩溃与哪个异常? – joao2fast4u 2014-10-19 22:54:21

+0

是的,抛出新的NumberFormatException(“Invalid int:\”“+ s +”\“”); – 2014-10-19 23:00:09

+0

请检查我的答案 – joao2fast4u 2014-10-19 23:08:34

回答

0

这是因为在删除所有EditTexts后,点击Button点击,您的afterTextChanged(Editable s)回调被调用。它期待您的EditText上的一些内容,但什么都没发现,因此投掷Exception,在parseInt()

你只需要添加该代码执行的条件,这就是,只有当文本的长度较大的是0。这样,你将防止回调清除EditTexts之后使你的应用程序崩溃。

所以,只要加入这行里面afterTextChanged(Editable s)

if(s.length()>0){ 

    //your existing code 

} 

我劝你只填补这些EditTexts与可转换为数字内容,或者你会得到Exception一次。

+1

非常感谢。 – 2014-10-19 23:18:34