猿问

为 TextView 赋值时的空值

我已经研究这个问题好几个小时了。我很确定我正确地将值 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);

            }

我可以看到它实际上是将值传递给 BowlerActivity


2019-07-09 20:47:06.694 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/Passing League Id: Passed Value is 2

2019-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 = 0

2019-07-09 20:47:06.786 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/GETALLBOWLERS-CNT: Number of elements in bowlerslist = 0

2019-07-09 20:47:06.786 8596-8596/ca.vogl.r.tenpinbowlingcompanion D/SAVEDLEAGUEID --->: 2

但是,当我尝试将值传递给名为 leagueId 的 TextView 时,它为空。


墨色风雨
浏览 106回答 2
2回答

MM们

正如我注意到的,您的 Dialog 窗口中有leagueIdtextView。因此,请确保您的布局文件tvLeagueId中有一个带有 id的 textView activity_bowlers_dialog。然后在你的openDialog函数中使用view.findViewById引用 textviewpublic void openDialog(final boolean shouldUpdate, final Bowler bowler, final int position) {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; View view = View.inflate(this, R.layout.activity_bowlers_dialog, null);&nbsp; &nbsp; &nbsp; &nbsp; leagueId = view.findViewById(R.id.tvLeagueId); // <-- Add this line&nbsp; &nbsp; &nbsp; &nbsp; AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BowlerActivity.this);&nbsp; &nbsp; &nbsp; &nbsp; alertDialogBuilderUserInput.setView(view);&nbsp; &nbsp; &nbsp; &nbsp; leagueId.setText(savedLeagueId);&nbsp; &nbsp; &nbsp; &nbsp; final EditText inputBowler = view.findViewById(R.id.etBowlerName);&nbsp; &nbsp; &nbsp; &nbsp; // rest of the code

慕田峪7331174

它与如何从返回 null 的 findViewById 中获取 TextView 的意图没有任何问题。java.lang.NullPointerException:尝试在* 空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” *
随时随地看视频慕课网APP

相关分类

Java
我要回答