亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為 TextView 賦值時的空值

為 TextView 賦值時的空值

墨色風雨 2023-03-17 14:08:52
我已經研究這個問題好幾個小時了。我很確定我正確地將值 saveLeagueId 分配給了 TextView。我想要做的是將一個值從 MainActivity 傳遞到 BowlerActivity。這是我的代碼,它使用意圖傳遞值;@Override            public void onClick(View view, final int position) {                int leagueId = leaguesList.get(position).getId();                String savedLeagueId = String.valueOf( leagueId );                Intent myIntent = new Intent(MainActivity.this, BowlerActivity.class);                Log.d("Passing League Id ", "Passed Value is " + leagueId);                myIntent.putExtra("leagueId", savedLeagueId);                startActivity(myIntent);                overridePendingTransition(0, 0);            }我可以看到它實際上是將值傳遞給 BowlerActivity2019-07-09 20:47:06.694 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/Passing League Id: Passed Value is 22019-07-09 20:47:06.777 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/GETALLBOWLERS-SQL: SQL used = >>>>SELECT  * FROM bowlers WHERE league_id = 'null' ORDER BY timestamp DESC<<<<2019-07-09 20:47:06.785 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/GETALLBOWLERS-CNT: Number of rows retrieved = 02019-07-09 20:47:06.786 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/GETALLBOWLERS-CNT: Number of elements in bowlerslist = 02019-07-09 20:47:06.786 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/SAVEDLEAGUEID --->: 2但是,當我嘗試將值傳遞給名為 leagueId 的 TextView 時,它為空。
查看完整描述

2 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

正如我注意到的,您的 Dialog 窗口中有leagueIdtextView。因此,請確保您的布局文件tvLeagueId中有一個帶有 id的 textView activity_bowlers_dialog。


然后在你的openDialog函數中使用view.findViewById引用 textview


public void openDialog(final boolean shouldUpdate, final Bowler bowler, final int position) {

        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());

        View view = View.inflate(this, R.layout.activity_bowlers_dialog, null);


        leagueId = view.findViewById(R.id.tvLeagueId); // <-- Add this line


        AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BowlerActivity.this);

        alertDialogBuilderUserInput.setView(view);


        leagueId.setText(savedLeagueId);

        final EditText inputBowler = view.findViewById(R.id.etBowlerName);


        // rest of the code


查看完整回答
反對 回復 2023-03-17
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

它與如何從返回 null 的 findViewById 中獲取 TextView 的意圖沒有任何問題。

java.lang.NullPointerException:嘗試在* 空對象引用上調用虛擬方法“void android.widget.TextView.setText(java.lang.CharSequence)” *


查看完整回答
反對 回復 2023-03-17
  • 2 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號