简体   繁体   English

ANDROID-将选定的微调框显示为另一个活动作为textview

[英]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.

相关问题 在一个活动中显示从微调器到另一个活动中的文本视图的选定值 - Show Selected value from spinner in one activity to the textview in another activity 在 textview 中的选定微调器中显示单词 - display the word in the selected spinner in the textview 如何在TextView中显示两个Spinner(在一个活动中显示)的值:Android - How to display value of two Spinner(present in one activity) in a TextView: Android 如何在 Android 中选择 Spinner 选项时显示不同的 textview - How to display different textview when Spinner option is selected in Android 如何在另一个活动中将微调器选定值显示到文本视图 - how to display the spinner selected values to text view in another activity 如何存储微调器选择的值并将其检索到另一个活动中以进行显示 - how to store spinner selected value and retrieve it in another activity for display 在另一个活动中使用微调器更改文本视图的颜色 - changing the color of a textview using a spinner in another activity 在另一个类TextView中显示微调框选择的项目 - Displaying Spinner selected item in another class TextView 在活动一中选择的微调器项目上,必须更改第二个活动中的textview - on spinner item selected in activity one, textview in the second activity must be changed 使用微调器选择项目填充TextView - Android - Populating TextView With Spinner Selected Item - Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM