Android, setText in TextView in popup window

1k views Asked by At

I have seen questions similar to this and tried they're solutions but for some reason I cannot get this to work. Everytime I try to set text it causes an error.

    LayoutInflater inflater = (LayoutInflater)Levels1_20.this.getSystemService(Context
            .LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.start_level_popup, null);

    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);
    int screenHeight = displayMetrics.heightPixels;
    int screenWidth = displayMetrics.widthPixels;


    final PopupWindow pw = new PopupWindow(layout, screenWidth, screenHeight, true);

    TextView bestScoreTextView = (TextView)layout.findViewById(R.id.bestScoreTextView);
    //Causes error
    bestScoreTextView.setText(userScore);
/*
    TextView bronzeRequiredTextView = (TextView)layout.findViewById(R.id.bronzeRequiredTextView);
    bronzeRequiredTextView.setText(bronzeRequired);
    TextView silverRequiredTextView = (TextView)layout.findViewById(R.id.silverRequiredTextView);
    silverRequiredTextView.setText(silverRequired);
    TextView goldRequiredTextView = (TextView)layout.findViewById(R.id.goldRequiredTextView);
    goldRequiredTextView.setText(goldRequired);
*/
    ((Button)layout.findViewById(R.id.backButton)).setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            pw.dismiss();
        }
    });

    ((Button)layout.findViewById(R.id.startButton)).setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent gameActivityIntent = new Intent(Levels1_20.this, GameActivity.class);
            startActivity(gameActivityIntent);
        }
    });
    pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
2

There are 2 answers

0
Gabriella Angelova On BEST ANSWER

If your userScore is from type int, you should change this line:

bestScoreTextView.setText(userScore);

to:

bestScoreTextView.setText(Integer.toString(userScore));

You are not able to set something different from String as text of a TextView

0
Vinothkumar Arputharaj On

Try to use string in setText() method.

bestScoreTextView.setText(userScore+"");