0

这就是我想要做的。首先,从单选按钮获取当前文本大小,然后将文本视图的文本大小设置为该值。但是,当我尝试时,总是会收到“空指针异常”错误。我对此进行了研究,但没有找到解决我问题的答案。单选按钮是 rb1,文本视图是 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);

    }
}

日志猫

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)

设置.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>
4

1 回答 1

1

我不完全确定你在这里做什么,但

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

返回null,因此您NPE在运行时会得到 a

 float default_font_size=rb1.getTextSize();

因为rb1null不在你膨胀的 xml 中

setContentView(R.layout.settings);

你必须inflate要么layout

`setContentView(R.layout.yourXML);

或使用充气机。您View的 s 存在于您的内部,因此如果不在您膨胀的内部layout,它们就会返回nulllayout

于 2013-06-11T18:54:58.313 回答