簡體   English   中英

在Android中的TextView上設置文本時出錯

[英]Error setting text on TextView in Android

我一直收到空指針錯誤,但我不知道為什么。 有什么很明顯的我想念嗎?

final Dialog d = new Dialog(Start.this);
        // dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
        d.requestWindowFeature((int) android.view.Window.FEATURE_NO_TITLE);

        d.setContentView(R.layout.popuptwo);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(d.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        d.show();

        d.getWindow().setAttributes(lp);
        TextView totaltxt = (TextView) findViewById(R.id.totaltxt);
            totaltxt.setText("Test text");

如果我刪除totaltxt.setText("Test text"); 那么程序不會崩潰。 有想法嗎?

文本視圖屬於對話框。.所以您應該使用對話框的視圖。

TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);

只需執行TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt); totaltxt.setText("Test text"); TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt); totaltxt.setText("Test text");

由於無法獲取對話框的視圖,

替換此行-

  TextView totaltxt = (TextView) findViewById(R.id.totaltxt);

這樣 -

  TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);

d是您的對話框對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM