Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Saurabh B
PLUS
Saurabh B
Courses Plus Student 2,880 Points

Getting error on simple radio button app

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

Daniel Hartin
Daniel Hartin
18,106 Points

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

Saurabh B
Saurabh B
Courses Plus Student 2,880 Points

The crash happens just after selecting the colour and clicking on next button.

In addition to the problems mentioned above, you also are probably getting a NullPointerException on the line

int forGettingId = mainRadioGroup.getCheckedRadioButtonId();

This is because you never initialize mainRadioGroup, yet its called in the onClick method. You should initialize it at the same time(and in the same manner) as radioButtonForRed and nextButton.

Saurabh B
PLUS
Saurabh B
Courses Plus Student 2,880 Points

Hi,

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

private RadioGroup mainRadioGroup;
private RadioButton radioButtonForRed;
private Button nextButton;
Saurabh B
PLUS
Saurabh B
Courses Plus Student 2,880 Points

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)