[英]ANDROID - Display selected spinner into another activity as textview
Dont Mark it As Duplicate Question, cz i've been tried to search any possible answer from Stackoverflow sublink
不要将其标记为重复问题,请尝试从Stackoverflow子链接中搜索任何可能的答案
i have 2 spinner in my MainActivity, then i use this code to save it inside button
setOnClickListener
code below : 我的MainActivity中有2个微调器,然后使用此代码将其保存在下面的
button
setOnClickListener
代码中:
Intent i= new Intent(MainActivity.this, Pertanyaan.class);
i.putExtra("dosen",String.valueOf(sp.getSelectedItem()));
i.putExtra("matkul",String.valueOf(sp2.getSelectedItem()));
startActivity(i);
then in my 2ndActivity, i've tried to call the key
i've declared like this: 然后在我的2ndActivity中,我试图调用这样声明的
key
:
TextView textdosen=(TextView)findViewById(R.id.txtdos);
TextView textmatkul=(TextView)findViewById(R.id.txtmat);
Bundle b=getIntent().getExtras();
String dosen=b.get("dosen").toString();
String matkul=b.get("matkul").toString();
textdosen.setText(dosen);
textmatkul.setText(matkul);
I've read and find out another Stackoverflow question, and almost of the answer using PutExtras()
/ PutExtra()
from intent
to save selected item from spinner, so i've tried it and getting error in my logcat
我已经阅读并找到了另一个Stackoverflow问题,并且从
intent
使用PutExtras()
/ PutExtra()
来保存微调器中的选定项目几乎是所有答案,因此我尝试了该问题并在logcat
出错
FATAL EXCEPTION: main
Process: flix.yudi.penilaian, PID: 28524
java.lang.RuntimeException: Unable to start activity ComponentInfo{flix.yudi.penilaian/flix.yudi.penilaian.Pertanyaan}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at flix.yudi.penilaian.Pertanyaan.onCreate(Pertanyaan.java:45)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
what should i do to fix the error?? 我该怎么做才能纠正错误? PLEASE HELP!
请帮忙!
Please make sure that below are not null 请确保以下内容不为空
TextView textdosen=(TextView)findViewById(R.id.txtdos);
TextView textmatkul=(TextView)findViewById(R.id.txtmat);
TextView textdosen=(TextView)findViewById(R.id.txtdos);
TextView textmatkul=(TextView)findViewById(R.id.txtmat);
String dosen = getIntent().getStringExtra("dosen");
String matkul = getIntent().getStringExtra("matkul");
textdosen.setText(dosen);
textmatkul.setText(matkul);
That's all you need . 这就是您所需要的。
In the logcat there is a following line: 在logcat中有以下一行:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
which basically means you are calling a setText
method on a null view. 这基本上意味着您在空视图上调用
setText
方法。 This could be caused by a number of different reasons. 这可能是由多种不同的原因引起的。 I think you should check out findViewByID returns null .
我认为您应该检查findViewByID返回null 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.