Can't resolve symbol 'setText' error

后端 未结 4 1649
情书的邮戳
情书的邮戳 2021-01-17 06:53

like the title says I\'m getting an error when trying to set a text view text from java in my android project, I can\'t see why.

    package com.codeherenow.         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 07:22

    You've got a couple issues.

    First, you can't do this here

    public TextView YT = (TextView) findViewById(R.id.Years);
    

    Because the layout hasn't been inflated yet so you can't look for your View witht findViewById(). You can declare it there

    public TextView YT;
    

    but you need to initialize it after you call setContentVie()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sicalculator);
    
        //can't do it before that line ^
        YT = (TextView) findViewById(R.id.Years);
    }
    

    And you are trying to call setText() outside of a method. Move that to some method. But you can't do it in onCreate() because Years isn't given a value. So you probably want it somewhere else. Maybe in onProgressChanged().

    Also, to follow Java naming conventions, you should name your variables by starting with lower case (yt or yT and years).

提交回复
热议问题