Попытка вызвать виртуальный метод RadioButton.gettext() для нулевого объекта. Ссылка

Я создаю страницу входа / регистрации в виде вкладок. Я получаю сообщение об ошибке на RadioButton от перенаправления текста проверенного радиокнопки.

Мой фрагмент Фрагмент:

package com.example.sqlitetest.fragments;

import android.app.AlertDialog.Builder;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.sqlitetest.R;

public class F_Register extends Fragment 
{
    EditText et1, et2, et3, et4, et5;
    RadioGroup rg;
    String radioButton;
    Button button;
    SQLiteDatabase db;
    View v;

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        v = inflater.inflate(R.layout.f_register, container, false);

        et1 = (EditText) v.findViewById(R.id.fr_et_name);
        et2 = (EditText) v.findViewById(R.id.fl_et_email);
        et3 = (EditText) v.findViewById(R.id.fl_et_pass);
        et4 = (EditText) v.findViewById(R.id.fr_et_city);
        et5 = (EditText) v.findViewById(R.id.fr_et_mobile);
        rg = (RadioGroup) v.findViewById(R.id.fr_radioGroup1);
        button = (Button) v.findViewById(R.id.fl_bt_login);

        db = getActivity().openOrCreateDatabase("SQLiteTest.db",
            android.content.Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS signup (usertype VARCHAR,name VARCHAR, email VARCHAR, password VARCHAR, city VARCHAR, mobile VARCHAR);");

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (et1.getText().toString().trim().length() == 0
                    || et2.getText().toString().trim().length() == 0
                    || et3.getText().toString().trim().length() == 0
                    || et4.getText().toString().trim().length() == 0
                    || et5.getText().toString().trim().length() == 0) {
                Toast.makeText(getActivity(), "Fill all required fields",
                        Toast.LENGTH_LONG).show();
            } else {
                 radioButton = ((RadioButton) v.findViewById(rg
                    .getCheckedRadioButtonId())).getText().toString(); 
                db.execSQL("INSERT INTO signup VALUES('" + radioButton
                        + "','" + et1.getText() + "','" + et2.getText()
                        + "','" + et3.getText() + "','" + et4.getText()
                        + "','" + et5.getText() + "');");
                showMessage("Success", "Record Added");
                clearText();
            }
        }
    });
    return v;
}

protected void clearText() {
    // TODO Auto-generated method stub
    et1.setText("");
    et2.setText("");
    et3.setText("");
    et4.setText("");
    et5.setText("");
}

protected void showMessage(String title, String message) {
    // TODO Auto-generated method stub
    Builder builder = new Builder(getActivity());
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

}

Мой XML-файл:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<RelativeLayout 
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <TextView
    android:id="@+id/fl_textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:text="I AM A"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

    <RadioGroup
    android:id="@+id/fr_radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fl_textView"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio_agent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Agent"
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/radio_owner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Owner"
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/radio_builder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Builder"
        android:textColor="#000000" />
</RadioGroup>

<TextView
    android:id="@+id/fr_textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fr_radioGroup1"
    android:text="NAME"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<EditText
    android:id="@+id/fr_et_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fr_textView1"
    android:hint="Enter Name" />

<TextView
    android:id="@+id/fl_textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fr_et_name"
    android:text="EMAIL"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<EditText
    android:id="@+id/fl_et_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:inputType="textEmailAddress"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fl_textView2"
    android:hint="Enter Email" />

<TextView
    android:id="@+id/fl_textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fl_et_email"
    android:text="PASSWORD"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<EditText
    android:id="@+id/fl_et_pass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fl_textView3"
    android:hint="ENTER PASSWORD" />

<TextView
    android:id="@+id/fr_textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fl_et_pass"
    android:text="CITY"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<EditText
    android:id="@+id/fr_et_city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fr_textView4"
    android:hint="Enter City" />

<TextView
    android:id="@+id/fr_textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fr_et_city"
    android:text="MOBILE NO"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<EditText
    android:id="@+id/fr_et_mobile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fl_textView"
    android:layout_alignStart="@+id/fl_textView"
    android:layout_below="@+id/fr_textView5"
    android:hint="Mobile No" />

<Button
    android:id="@+id/fl_bt_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/fr_et_mobile"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#FF3300"
    android:text="SIGN UP" />

</RelativeLayout>
</ScrollView>

Logcat:

03-09 16:13:31.230: W/System(18195): ClassLoader referenced unknown path:/data/app/com.example.sqlitetest-1/lib/x86_64
03-09 16:13:31.770: D/OpenGLRenderer(18195): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-09 16:13:31.780: D/(18195): HostConnection::get() New Host Connection established 0x7f104eb65180, tid 18195
03-09 16:13:32.390: D/(18195): HostConnection::get() New Host Connection established 0x7f1057e84580, tid 18210
03-09 16:13:32.390: I/OpenGLRenderer(18195): Initialized EGL, version 1.4
03-09 16:13:32.450: W/EGL_emulation(18195): eglSurfaceAttrib not implemented
03-09 16:13:32.450: W/OpenGLRenderer(18195): Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f105e6ac940, error=EGL_SUCCESS
03-09 16:13:32.500: I/Choreographer(18195): Skipped 38 frames!  The application may be doing too much work on its main thread.
03-09 16:14:02.000: D/AndroidRuntime(18195): Shutting down VM
03-09 16:14:02.000: E/AndroidRuntime(18195): FATAL EXCEPTION: main
03-09 16:14:02.000: E/AndroidRuntime(18195): Process:com.example.sqlitetest, PID: 18195
03-09 16:14:02.000: E/AndroidRuntime(18195): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
03-09 16:14:02.000: E/AndroidRuntime(18195):    at com.example.sqlitetest.fragments.F_Register$1.onClick(F_Register.java:61)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at android.view.View.performClick(View.java:5198)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at android.view.View$PerformClick.run(View.java:21147)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at android.os.Handler.handleCallback(Handler.java:739)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at android.os.Looper.loop(Looper.java:148)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at android.app.ActivityThread.main(ActivityThread.java:5417)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at java.lang.reflect.Method.invoke(Native Method)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-09 16:14:02.000: E/AndroidRuntime(18195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Если я удалю строку:

radioButton = ((RadioButton)v.findViewById(rg.getCheckedRadioButtonId())).getText().toString();

в операторе else код отлично работает с полем usertype в базе данных со значением NULL. Я хочу получить текст переключателя, но безуспешно.

Пожалуйста, помогите мне с проблемой здесь.

0 ответов

Другие вопросы по тегам