尝试从radioButton(Android)获取值时java.lang.NullPointerException

时间:2014-05-20 11:11:27

标签: java android

我正在尝试从radioButton获取字符串值,但是当我尝试它时会在LogCat上显示此错误。我似乎无法弄清楚如何解决它......这些是代码:

    protected void onCreate(Bundle konsumsi) {
        super.onCreate(konsumsi);
        radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);
        setContentView(R.layout.aktivitas_user);
        usernameP = Login.usernameP;
        System.out.println(usernameP);

        buttonDone();

    }
public void buttonDone() {
        selesai = (Button) findViewById(R.id.buttonProses);
        selesai.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 int selectedId = radioAktivitasGroup.getCheckedRadioButtonId();     
                    radioAktivitasButton = (RadioButton) findViewById(selectedId);
                    String Aktivitas = radioAktivitasButton.getText().toString();
            }
        });
    }  

错误:

    05-20 07:01:07.858: E/AndroidRuntime(3179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ta.nutrifact/com.ta.nutrifact.AktivitasUser}: java.lang.NullPointerException
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.os.Looper.loop(Looper.java:137)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at java.lang.reflect.Method.invoke(Method.java:525)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at dalvik.system.NativeStart.main(Native Method)
05-20 07:01:07.858: E/AndroidRuntime(3179): Caused by: java.lang.NullPointerException
05-20 07:01:07.858: E/AndroidRuntime(3179):     at com.ta.nutrifact.AktivitasUser.onCreate(AktivitasUser.java:71)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.Activity.performCreate(Activity.java:5133)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-20 07:01:07.858: E/AndroidRuntime(3179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-20 07:01:07.858: E/AndroidRuntime(3179):     ... 11 more

我在另一个班级做了完全一样的工作,但是工作却没有。任何帮助,将不胜感激。谢谢

5 个答案:

答案 0 :(得分:5)

撤销陈述

 radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);
 setContentView(R.layout.aktivitas_user);

 setContentView(R.layout.aktivitas_user);
 radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);

答案 1 :(得分:0)

变化

  radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);// null pointer boz you call it before initialize xml file.
  setContentView(R.layout.aktivitas_user);

setContentView(R.layout.aktivitas_user); //set layout first 
radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);

答案 2 :(得分:0)

移动此

radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);

之后

setContentView(R.layout.aktivitas_user);

像,

setContentView(R.layout.aktivitas_user);
radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);

答案 3 :(得分:0)

尝试以下代码: -

    radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);
    setContentView(R.layout.aktivitas_user);

    // this right way to do
    setContentView(R.layout.aktivitas_user);
    radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);

显示空指针异常,因为您在布局添加之前初始化视图。

答案 4 :(得分:0)

首先使用setContentView,然后使用findViewById