我无法在onClick()中使用setText()

时间:2018-08-11 15:36:35

标签: android

enter image description here我正在尝试使用setText,但是当它进入onClick时,它将无法正常工作。在代码中,在onCreate而不是onClick上很好,但是在onClick上使用setText时(在onCreate中则为-),当告诉我无法解决此功能时显示红色

&it>

ToggleButton gpsToggleButton;
TextView  gpsTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //permission area
    //get user permission using of gps
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        gpsuserPermission();
    }

    gpsToggleButton = (ToggleButton) findViewById(R.id.toggleButton_gps);
    gpsTextView = (TextView) findViewById(R.id.textView_gps);
    gpsTextView.setText("why it doesn't work?");
    final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    gpsToggleButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View gpsTextView) {

            try{
                if(gpsToggleButton.isChecked()){
                    gpsTextView.setText();/// THIS PART CAUSE ERROR[
                    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
                    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 10, locationListener);
                    gpsTextView.setText();/// THIS PART CAUSE ERROR[enter image description here][1]
                }

            }catch (SecurityException ex){
                    gpsTextView.setTextAlignment());
            }
        }

    });




}

3 个答案:

答案 0 :(得分:1)

 ((ToggleButton)gpsTextView).setText("");

 gpsToggleButton.setText("");

答案 1 :(得分:0)

更改标题:

SelectedIndex

具有:

namespace PresentationWPF.View.Converters
{
    public class IndexToMonthConverter : IValueConverter
    {
        private DateTime _dateTime;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert Month in 'value(DateTime)' ==> Index 0 to 11
            if (value is DateTime b)
            {
                _dateTime = b;
                return b.Month - 1;
            }

            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert 'value(int)' 0 to 11 ==> Month number
            if(value is int b)
                return new DateTime(1,b + 1,1);

            return _dateTime;
        }
    }
}

它是单击的切换按钮,而不是public void onClick(View gpsTextView)
public void onClick(View v) 方法TextView内部的当前代码中,该视图被视为不同的视图,单击该视图意味着切换按钮不是实际的onClick()

答案 2 :(得分:0)

要解决此问题,您确实需要结合 @ItzikSamara @mTak 提供的解决方案。

您已经定义了一个类变量“ gpsTextView”,以指向您要引用的TextView对象。但是,在onClick()方法中,您使用与参数名称相同的变量名称在本地覆盖了此变量:
public void onClick(View gpsTextView)-> public void onClick(View v)
按照mTak的建议

通过将参数名称更改为其他名称(在本例中为“ v”),您将可以访问类变量“ gpsTextView”,并且可以使用setText()方法设置文本值。但是,实际上,您应该将文本设置为类似空字符串之类的东西-> gpsTextView.setText("") ItzikSamara建议

将代码更改为此:

public void onClick(View v) {
    try{
        if(gpsToggleButton.isChecked()){
            gpsTextView.setText("");
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 10, locationListener);
            //gpsTextView.setText(); -- No need to do this twice!
        }
    }catch (SecurityException ex){
        Log.e("MyActivity", ex.getMessage());
    }
}