Android, setText in TextView in popup window

Android, setText in TextView in popup window

我看到过与此类似的问题并尝试了它们的解决方案,但由于某种原因我无法让它工作。每次我尝试设置文本时都会导致错误。

    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);

尝试在 setText() 方法中使用字符串。

bestScoreTextView.setText(userScore+"");

如果您的 userScore 来自 int 类型,您应该更改此行:

bestScoreTextView.setText(userScore);

至:

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

您无法将不同于 String 的内容设置为 TextView

的文本