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)

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>
4

1 に答える 1

1

あなたがここで何をしようとしているのか正確にはわかりませんが、

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

を返すnullので、NPE実行時に

 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 に答える