0

this is what I am trying to do. First, get the current text size from a radio button, and then set the text size of a text view to that value. however, when I try I always get a "null pointer exception" error. i have researched this and not found an answer that fixes my problem. The radio button is rb1 and the textview is sampleText.

public class Settings extends Activity {

RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioGroup ans;
TextView sampleT;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    ans= (RadioGroup) findViewById(R.id.answers);
    rb1= (RadioButton) findViewById(R.id.answer1);
    rb2= (RadioButton) findViewById(R.id.answer2);
    rb3= (RadioButton) findViewById(R.id.answer3);
    sampleT= (TextView) findViewById(R.id.sampleText);

     float default_font_size=rb1.getTextSize(); //this is the line with error
     sampleT.setTextSize(default_font_size);

    }
}

Logcat

06-11 14:37:31.809  15625-15625/com.nick.simplequiz            E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nick.simplequiz/com.nick.simplequiz.Settings}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$600(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5454)
        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:1029)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.nick.simplequiz.Settings.onCreate(Settings.java:32)
        at android.app.Activity.performCreate(Activity.java:5066)

settings.xml:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Answer Choice Text Size"
        android:id="@+id/sampleText"
        android:layout_gravity="left|center_vertical"
        android:textStyle="bold"
        android:textSize="25dp"/>

    <SeekBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_gravity="center"
        android:progress="50"/>

    </LinearLayout>
8
  • Either rb1 or sampleText is null. Make sure you initialize these. Commented Jun 11, 2013 at 18:26
  • yes i checked and both are initialized before this Commented Jun 11, 2013 at 18:32
  • Are you sure? Did you check the values at runtime, either by logging them or using the debug mode? Commented Jun 11, 2013 at 18:34
  • float default_font_size=rb1.getTextSize(); is line 32 Commented Jun 11, 2013 at 18:49
  • Yes, see my previous comment Commented Jun 11, 2013 at 18:50

1 Answer 1

1

I'm not exactly sure what you're going for here but

rb1= (RadioButton) findViewById(R.id.answer1);

returns null so you get a NPE when you run

 float default_font_size=rb1.getTextSize();

because rb1 is null since it isn't in the xml you inflate with

setContentView(R.layout.settings);

You have to inflate a layout either with

`setContentView(R.layout.yourXML);

or with an inflater. Your Views exist within your layout so they return null if not within a layout you have inflated

Sign up to request clarification or add additional context in comments.

4 Comments

ok i see what you are saying. ill have to figure out another way to do what im trying to do. thanks for helping me
You can't without inflating the xml file they are in. I'm not sure why you are trying to do it this way but you can't
what i'm really trying to do it change the text size of text that is used in my app but it is in a different layout. i want to be able to change it from a settings layout. there must be a simpler way
@ez4nick You can change to a SettingsActivity, make it a dialog or whatever so the user doesn't feel as though they have left the current screen, and save the value in SharedPreferences then read that value to set the text size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.