尝试在空对象引用上调用虚拟方法'void android.widget.RadioGroup.check(int)'

时间:2019-07-12 10:09:26

标签: android

我试图在公共课堂上存储四个单选按钮的会话     SharedPrefManager和家庭活动对我不起作用

  

java.lang.NullPointerException:尝试调用虚拟方法   空对象引用上的“ void android.widget.RadioGroup.check(int)”           在themedicall.com.Home.onCreate(Home.java:257)

 public class SharedPrefManager {

 private static SharedPrefManager mInstance;
SharedPreferences.Editor editor;
SharedPreferences sharedPrefs;

public static synchronized SharedPrefManager getLoginInstance(Context context) {
    if (mInstance == null) {
        mInstance = new SharedPrefManager(context);
    }
    return mInstance;
}

private SharedPrefManager(Context context) {
    sharedPrefs = context.getSharedPreferences("shared_pref", Context.MODE_PRIVATE);
}

public void saveData(String hospital) {
    editor = sharedPrefs.edit();
    if (!hospital.equalsIgnoreCase("")) editor.putString("hospital", hospital);

    editor.apply();
}
public boolean isradioChecked() {
    String hospital = sharedPrefs.getString("hospital", "");
    if (hospital.equals("")) {
        return false;
    } else {
        return true;
    }
}
public boolean isDrugLogin() {
    String Drugs = sharedPrefs.getString("Drugs", "");
    if (Drugs.equals("")) {
        return false;
    } else {
        return true;
    }
}
public void logout() {
    editor = sharedPrefs.edit();
    editor.clear();
    editor.apply();
}

家庭活动

    public class Home extends NavigationDrawer implements
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener, 
    SearchView.OnQueryTextListener {


EditText mainSearchView;
public static final int REQUEST_PERMISSION_CODE = 30;
ProgressDialog progressDialog;
private static final String TAG = "Home";
String cancel_req_tag = "Home";
Boolean locationApiHasRun = false;

public static Double lat, lang;
public static LatLng userCurrentLocation;

GoogleApiClient mGoogleApiClient;
Location mLastLocation;
LocationRequest mLocationRequest;
public static String city, state, country, postalCode, knownName;
SharedPreferences sharedPreferencesCityAndLatLng, sharedPreferencesForBloodCurrentCity;
public static SharedPreferences.Editor cityAndLatLngEditor, bloodCurrentCity;

JSONObject jsonObject;
JSONArray jsonArray;
Button locationFilter;
CircleImageView userIcon;
ImageView searchViewImg;
ImageView doctorFilterImage;
ImageView WhatsappBtn;
ImageView WebBtn;
ImageView AccountBtn;
RelativeLayout.LayoutParams params;
ImageView iv_crosee;
RelativeLayout rl_for_drawer_icon;
ImageView im_up_arrow, iv_touch;
RelativeLayout rl_full_screen_view;



SearchView search_view;



int radioSelectedId;
RadioButton radioButton;
private Timer timer;
JSONObject object;
JSONArray hospitalArray;



AutoCompleteTextView autoCompleteSearch;

Handler mHandler;
String radioButtonText = "Doctors";
private String textToSearch;
private String filter;
ProgressBar advanceSearchBar;

Dialog networkDialog;

RelativeLayout homeMainLayout;
MyReceiverForImageUploaded myReceiverForImageUploaded;
SharedPreferences sharedPreferencesFirstTimeGuied;

Button drugBookBtn;
Button studyRoomBtn;

RadioGroup mainSearchRadioGroup, mainSearchRadioGroup2;

String generic = "Generic";
String brand = "Brand";
String company = "Company";
Boolean genericBool;
Boolean brandBool;
Boolean companyBool;
String company_id;
String company_name;
String brand_name;
String generics_id;
String generics_name;
String potency;
String potencymajor;
String brand_detail_id;
String packing;
String drug_form;
String price;
String generic_name;
String drugId;
JSONObject company_jsonObject;
JSONObject form_jsonObject;
JSONArray drugs_JsonArray;
JSONArray brand_details;
Boolean companyloaded = false;
Boolean genericsloaded = false;
Boolean brandloaded = false;
String type = "Brand";
private ProgressBar bar;
private int requestCount = 1;
int last_page = 0;

BroadcastReceiver checkConectivity;

DataBaseAccess db;
RadioButton hospital_radio;

RadioGroup radio_group;
 @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (SharedPrefManager.getLoginInstance(Home.this).isradioChecked()) {

            radio_group.check(R.id.hospital_radio);
            radioButtonText="Hospitals";
            mainSearchRadioGroup.clearCheck();
            openSearchViewDialog();


    }else if 
(SharedPrefManager.getLoginInstance(Home.this).isDrugLogin())
    {

            radio_group.check(R.id.brand_radio);
            radioButtonText="Drugs";
            openSearchViewDialog();
            mainSearchRadioGroup.clearCheck();
        }

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 getSupportActionBar().setCustomView
(R.layout.custom_actionbar_navigation_drawer);
    customActionBarView=getSupportActionBar().getCustomView();
    getSupportActionBar().setElevation(0);

    getSupportActionBar().setBackgroundDrawable(new 

 ColorDrawable(getResources()
 .getColor(R.color.landingScreenBackgroundColor)));

    LayoutInflater layoutInflater=(LayoutInflater) 
    this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.inflate(R.layout.activity_home, null, 
   false);
    drawer.addView(view, 0);

   radio_group = (RadioGroup) findViewById(R.id.radio_group);
    hospital_radio= (RadioButton) findViewById(R.id.hospital_radio);
    //radio_group.check(R.id.doctor_radio);
    radio_group.setOnCheckedChangeListener(listener1);
     mainSearchRadioGroup.setOnCheckedChangeListener(listener1);
   }
   private RadioGroup.OnCheckedChangeListener listener1 = new 
   RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {


        if (group.getCheckedRadioButtonId() == R.id.doctor_radio) {

            radioButtonText = "Doctors";


        } else if (group.getCheckedRadioButtonId() == 
 R.id.hospital_radio) {

            radioButtonText = "Hospitals";


        } else if (group.getCheckedRadioButtonId() == R.id.brand_radio) {

            radioButtonText = "Drugs";
        } else if (group.getCheckedRadioButtonId() == R.id.generic_radio) 
   {

            radioButtonText = "Generics";

            //saveData(radioButtonText);

       }else {


 SharedPrefManager.getLoginInstance(Home.this).saveData(radioButtonText);
            Log.e("tag", "radio button in text in main search : " + 
 radioButtonText);
        }


    }
   };
           public void openSearchViewDialog() {
    autoCompleteSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(final Editable editable) {


            if (radioButtonText.equals("Doctors")) {

                if (editable.toString().length() >= 3) {

                    Log.e("tag", "auto complete text : " + 
 editable.toString());
                    Log.e("tag", "doctor radio button selected: ");
                    doctorNameFilter.clear();
                    useHandler(editable.toString(), "Doctors");

                } else {
                    Log.e("tag", "web service not call " + 
 editable.toString());
                }
            } else if (radioButtonText.equals("Hospitals")) {
                if (editable.toString().length() >= 3) {
                    Log.e("tag", "auto complete text : " + 
  editable.toString());
                    Log.e("tag", "radio button in text in main search : " 
 + radioButtonText);

                    HospitalNameFilter.clear();
                    useHandler(editable.toString(), "Hospitals");
                } else {
                    Log.e("tag", "web service not call " + 
  editable.toString());
                }

            } else if (radioButtonText.equals("Drugs")) {

                if (editable.toString().length() >= 3) {
                    Log.e("tag", "auto complete text : " + 
 editable.toString());
                    Log.e("tag", "radio button in text in main search : " 
  + radioButtonText);

                    brandNamesFilter.clear();
                    useHandler(editable.toString(), "Drugs");
                } else {
                    Log.e("tag", "web service not call " + 
    editable.toString());
                }

            } else if (radioButtonText.equals("Generics")) {

                if (editable.toString().length() >= 3) {
                    Log.e("tag", "auto complete text : " + 
   editable.toString());
                    Log.e("tag", "radio button in text in main search : " 
  + radioButtonText);

                    genericNamesFilter.clear();
                    useHandler(editable.toString(), "Generics");
                } else {
                    Log.e("tag", "web service not call " + 
  editable.toString());
                }

            }

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int 
  i1, int i2) {


        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, 
    int i1, int i2) {
            //  Toast.makeText(Hospitals.this, "text in beforeTextChanged 
  "+charSequence.toString(), Toast.LENGTH_SHORT).show();

        }


    });

2 个答案:

答案 0 :(得分:0)

您尝试在初始化之前设置radio_group。看看

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 getSupportActionBar().setCustomView
(R.layout.custom_actionbar_navigation_drawer);
    customActionBarView=getSupportActionBar().getCustomView();
    getSupportActionBar().setElevation(0);

    getSupportActionBar().setBackgroundDrawable(new 

 ColorDrawable(getResources()
 .getColor(R.color.landingScreenBackgroundColor)));

    LayoutInflater layoutInflater=(LayoutInflater) 
    this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.inflate(R.layout.activity_home, null, 
   false);
    drawer.addView(view, 0);

   radio_group = (RadioGroup) findViewById(R.id.radio_group);
    hospital_radio= (RadioButton) findViewById(R.id.hospital_radio);
    //radio_group.check(R.id.doctor_radio);
    radio_group.setOnCheckedChangeListener(listener1);
     mainSearchRadioGroup.setOnCheckedChangeListener(listener1);
if (SharedPrefManager.getLoginInstance(Home.this).isradioChecked()) {

            radio_group.check(R.id.hospital_radio);
            radioButtonText="Hospitals";
            mainSearchRadioGroup.clearCheck();
            openSearchViewDialog();


    }else if 
(SharedPrefManager.getLoginInstance(Home.this).isDrugLogin())
    {

            radio_group.check(R.id.brand_radio);
            radioButtonText="Drugs";
            openSearchViewDialog();
            mainSearchRadioGroup.clearCheck();
        }
   }


   private RadioGroup.OnCheckedChangeListener listener1 = new 
   RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {


        if (group.getCheckedRadioButtonId() == R.id.doctor_radio) {

            radioButtonText = "Doctors";


        } else if (group.getCheckedRadioButtonId() == 
 R.id.hospital_radio) {

            radioButtonText = "Hospitals";


        } else if (group.getCheckedRadioButtonId() == R.id.brand_radio) {

            radioButtonText = "Drugs";
        } else if (group.getCheckedRadioButtonId() == R.id.generic_radio) 
   {

            radioButtonText = "Generics";

            //saveData(radioButtonText);

       }else {


 SharedPrefManager.getLoginInstance(Home.this).saveData(radioButtonText);
            Log.e("tag", "radio button in text in main search : " + 
 radioButtonText);
        }


    }
   };
           public void openSearchViewDialog() {
    autoCompleteSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(final Editable editable) {


            if (radioButtonText.equals("Doctors")) {

                if (editable.toString().length() >= 3) {

                    Log.e("tag", "auto complete text : " + 
 editable.toString());
                    Log.e("tag", "doctor radio button selected: ");
                    doctorNameFilter.clear();
                    useHandler(editable.toString(), "Doctors");

                } else {
                    Log.e("tag", "web service not call " + 
 editable.toString());
                }
            } else if (radioButtonText.equals("Hospitals")) {
                if (editable.toString().length() >= 3) {
                    Log.e("tag", "auto complete text : " + 
  editable.toString());
                    Log.e("tag", "radio button in text in main search : " 
 + radioButtonText);

                    HospitalNameFilter.clear();
                    useHandler(editable.toString(), "Hospitals");
                } else {
                    Log.e("tag", "web service not call " + 
  editable.toString());
                }

            } else if (radioButtonText.equals("Drugs")) {

                if (editable.toString().length() >= 3) {
                    Log.e("tag", "auto complete text : " + 
 editable.toString());
                    Log.e("tag", "radio button in text in main search : " 
  + radioButtonText);

                    brandNamesFilter.clear();
                    useHandler(editable.toString(), "Drugs");
                } else {
                    Log.e("tag", "web service not call " + 
    editable.toString());
                }

            } else if (radioButtonText.equals("Generics")) {

                if (editable.toString().length() >= 3) {
                    Log.e("tag", "auto complete text : " + 
   editable.toString());
                    Log.e("tag", "radio button in text in main search : " 
  + radioButtonText);

                    genericNamesFilter.clear();
                    useHandler(editable.toString(), "Generics");
                } else {
                    Log.e("tag", "web service not call " + 
  editable.toString());
                }

            }

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int 
  i1, int i2) {


        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, 
    int i1, int i2) {
            //  Toast.makeText(Hospitals.this, "text in beforeTextChanged 
  "+charSequence.toString(), Toast.LENGTH_SHORT).show();

        }


    });

答案 1 :(得分:0)

在使用无线电组之前,您错过了初始化。看看您使用的onCreateMethod

if (SharedPrefManager.getLoginInstance(Home.this).isradioChecked()) {

            radio_group.check(R.id.hospital_radio);
            radioButtonText="Hospitals";
            mainSearchRadioGroup.clearCheck();
            openSearchViewDialog();


    }

但是您在调用 check radio_group = (RadioGroup) findViewById(R.id.radio_group);

后初始化了 radio_group

因此,在致电setCheck();

之前将上面的行移动