Java android radio button แจ งเต อน คนละกล ม

Please help me. Whats wrong with the code. The app crashes and I get msg as "app has unfortunately stopped"

package com.example.saurabhb.gettingcolor;

import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast;

public class AskColorMainActivity extends ActionBarActivity {

private RadioGroup mainRadioGroup; private RadioButton radioButtonForRed; private Button nextButton;

@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_ask_color_main); radioButtonForRed = (RadioButton) findViewById(R.id.radioButtonForRed); nextButton = (Button) findViewById(R.id.nextButton); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int forGettingId = mainRadioGroup.getCheckedRadioButtonId(); radioButtonForRed = (RadioButton) findViewById(forGettingId); String forIntent = radioButtonForRed.getText().toString(); Toast.makeText(AskColorMainActivity.this, forIntent, Toast.LENGTH_LONG).show(); Intent intent = new Intent(AskColorMainActivity.this, AboutTheColor.class); intent.putExtra("colour", forIntent); startActivity(intent); } }); }

on next activity:

package com.example.saurabhb.gettingcolor;

import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView;

public class AboutTheColor extends ActionBarActivity {

private TextView pageTwoText; private String lastTimeIntent;

@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_about_the_color); nextButton = (Button) findViewById(R.id.nextButton); Intent intent = getIntent(); lastTimeIntent = intent.getStringExtra("colour"); pageTwoText.setText("Hello"); } }

3 Answers

Hi Saurabh

Just a heads up, it may be better if you can try and explain where exactly the fault occurs, just so we know where to look.

I can see three faults:-

  • in your onClick() method of the class AskColorMainActivity you have declared your radioButtonforRed again which is not needed (wont cause the crash though)

-Both classes extend ActionBarActivity but they are not using the menu(s) so it may be better to extend Activity (Just so people know which methods to expect, again wont cause the crash)

  • The line pageTwoText.setText("Hello"); in the AboutTheColor class will cause an error as you haven't defined it anywhere you need to link it to the view in your XML file (I don't know if you code gets this far though).

Hope this helps

Daniel

PLUS

Hi,

I had initialized but still I am getting error: public class AskColorMainActivity extends ActionBarActivity {

private RadioGroup mainRadioGroup; private RadioButton radioButtonForRed; private Button nextButton;

PLUS

Please see the log cat and guide me...

04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 12208: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 9779: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V 04-24 18:05:46.001 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e 04-24 18:05:46.041 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 04-24 18:05:46.041 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 12211: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 04-24 18:05:46.041 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007 04-24 18:05:46.051 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 04-24 18:05:46.051 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 393: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 04-24 18:05:46.051 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 04-24 18:05:46.051 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 04-24 18:05:46.051 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 415: Landroid/content/res/TypedArray;.getType (I)I 04-24 18:05:46.051 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 04-24 18:05:46.061 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable 04-24 18:05:46.061 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 356: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 04-24 18:05:46.061 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 04-24 18:05:46.061 19299-19299/com.example.saurabhb.gettingcolor I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity 04-24 18:05:46.061 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ VFY: unable to resolve virtual method 358: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 04-24 18:05:46.061 19299-19299/com.example.saurabhb.gettingcolor D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 04-24 18:05:47.523 19299-19303/com.example.saurabhb.gettingcolor D/dalvikvm﹕ GC_CONCURRENT freed 203K, 11% free 9620K/10759K, paused 117ms+36ms, total 194ms 04-24 18:05:48.374 19299-19299/com.example.saurabhb.gettingcolor D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so 04-24 18:05:48.394 19299-19299/com.example.saurabhb.gettingcolor D/﹕ mem_init ++ 04-24 18:05:48.394 19299-19299/com.example.saurabhb.gettingcolor D/﹕ gHwMemAllocator client 3 04-24 18:05:48.394 19299-19299/com.example.saurabhb.gettingcolor D/﹕ **** Using ION allocator **** 04-24 18:05:48.394 19299-19299/com.example.saurabhb.gettingcolor D/﹕ registered SIGUSR1[10] for pid[19299] 04-24 18:05:48.394 19299-19299/com.example.saurabhb.gettingcolor D/﹕ HwMemAllocatorImpl Static Counters 0 0 04-24 18:05:48.394 19299-19299/com.example.saurabhb.gettingcolor D/﹕ HwMemAllocatorImpl[4ad7dd5c] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0] 04-24 18:05:48.414 19299-19299/com.example.saurabhb.gettingcolor D/﹕ mem_init 4ad7dd5c-- 04-24 18:05:48.414 19299-19299/com.example.saurabhb.gettingcolor D/ION﹕ config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608) 04-24 18:05:48.414 19299-19299/com.example.saurabhb.gettingcolor D/﹕ Waiting for mm thread to come up 04-24 18:05:48.424 19299-19299/com.example.saurabhb.gettingcolor D/﹕ Waiting for mm thread to come up 04-24 18:05:48.424 19299-19338/com.example.saurabhb.gettingcolor D/﹕ mm_device_thread starting 04-24 18:05:48.444 19299-19299/com.example.saurabhb.gettingcolor D/HAWAII_EGL﹕ eglCreateContext() config: 35 context: 0x4cf9f918, VC context 1, Thread 19299 04-24 18:05:48.454 19299-19299/com.example.saurabhb.gettingcolor D/HAWAII_EGL﹕ Set SWAP INTERVAL 0 04-24 18:05:48.454 19299-19299/com.example.saurabhb.gettingcolor D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4cf9f938, VC surface: 1, Thread: 19299 04-24 18:05:48.454 19299-19299/com.example.saurabhb.gettingcolor D/HAWAII_EGL﹕ eglMakeCurrent(0x4cf9f918, 0x4cf9f938, 0x4cf9f938) Thread: 19299 04-24 18:05:48.474 19299-19299/com.example.saurabhb.gettingcolor D/OpenGLRenderer﹕ Enabling debug mode 0 04-24 18:05:51.757 19299-19299/com.example.saurabhb.gettingcolor D/AndroidRuntime﹕ Shutting down VM 04-24 18:05:51.757 19299-19299/com.example.saurabhb.gettingcolor W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416c82a0) 04-24 18:05:51.847 19299-19299/com.example.saurabhb.gettingcolor E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.example.saurabhb.gettingcolor.AskColorMainActivity$1.onClick(AskColorMainActivity.java:32) at android.view.View.performClick(View.java:4275) at android.view.View$PerformClick.run(View.java:17434) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:177) at android.app.ActivityThread.main(ActivityThread.java:4947) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) at dalvik.system.NativeStart.main(Native Method)

Toplist

โพสต์ล่าสุด

แท็ก

ไทยแปลอังกฤษ แปลภาษาไทย โปรแกรม-แปล-ภาษา-อังกฤษ พร้อม-คำ-อ่าน lmyour แปลภาษา แปลภาษาอังกฤษเป็นไทย pantip ไทยแปลอังกฤษ ประโยค แอพแปลภาษาอาหรับเป็นไทย ห่อหมกฮวกไปฝากป้าmv ระเบียบกระทรวงการคลังว่าด้วยการจัดซื้อจัดจ้างและการบริหารพัสดุภาครัฐ พ.ศ. 2560 แปลภาษาอาหรับ-ไทย Terjemahan พจนานุกรมศัพท์ทหาร หยน แปลภาษา มาเลเซีย ไทย Bahasa Thailand ข้อสอบภาษาอังกฤษ พร้อมเฉลย pdf บบบย tor คือ จัดซื้อจัดจ้าง การ์ดแคปเตอร์ซากุระ ภาค 4 ชขภใ ยศทหารบก เรียงลําดับ ห่อหมกฮวกไปฝากป้า หนังเต็มเรื่อง เขียน อาหรับ แปลไทย แปลภาษาอิสลามเป็นไทย Google map กรมพัฒนาฝีมือแรงงาน อบรมออนไลน์ กระบวนการบริหารทรัพยากรมนุษย์ 8 ขั้นตอน ข้อสอบคณิตศาสตร์ พร้อมเฉลย ค้นหา ประวัติ นามสกุล อาจารย์ ตจต แจ้ง ประกาศ น้ำประปาไม่ไหล แปลบาลีเป็นไทย แปลภาษา ถ่ายรูป แปลภาษาจีน แปลภาษามลายู ยาวี โรงพยาบาลภมูพลอดุยเดช ที่อยู่ Google Drive Info TOR คือ กรมพัฒนาฝีมือแรงงาน ช่างไฟฟ้า กรมพัฒนาฝีมือแรงงาน อบรมฟรี 2566 กลยุทธ์ทางการตลาด มีอะไรบ้าง การบริหารทรัพยากรมนุษย์ มีอะไรบ้าง การประปาส่วนภูมิภาค การ์ดแคปเตอร์ซากุระ ภาค 3 ขขขขบบบยข ่ส ข่าว น้ำประปา วันนี้ ข้อสอบโอเน็ต ม.6 มีกี่ตอน ตารางธาตุ ประปาไม่ไหล วันนี้