我在类uncheckAll()
内的方法中收到空对象引用时遇到问题HomeActivity
。但问题是我已经在MyPreferenceActivity
类中引用了两个对象( checkboxPreferenceEvent 和 checkBoxPreferenceQuote )。希望你们能帮我解决这个问题。
谢谢..
checkBoxPreferenceEvent = checkBoxPreferenceArrayList.get(0);
checkBoxPreferenceQuote = checkBoxPreferenceArrayList.get(1);
错误
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.CheckBoxPreference.setChecked(boolean)' on a null object reference
at com.example.user.uniselic.MyPreferencesActivity.uncheckAll(MyPreferencesActivity.java:51)
at com.example.user.uniselic.HomeActivity.onOptionsItemSelected(HomeActivity.java:193)
at android.app.Activity.onMenuItemSelected(Activity.java:2928)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
MyPreferencesActivity.java
package com.example.user.uniselic;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.TwoStatePreference;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.ArrayList;
/**
* Created by User on 11/1/2016.
*/
public class MyPreferencesActivity extends PreferenceActivity{
ArrayList<CheckBoxPreference> checkBoxPreferenceArrayList;
CheckBoxPreference checkBoxPreferenceQuote;
CheckBoxPreference checkBoxPreferenceEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyPreferenceFragment myPreferenceFragment = new MyPreferenceFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content,myPreferenceFragment).commit();
checkBoxPreferenceArrayList = myPreferenceFragment.getCheckBoxPreferenceArrayListInner();
if(!checkBoxPreferenceArrayList.isEmpty()){
checkBoxPreferenceEvent = checkBoxPreferenceArrayList.get(0);
checkBoxPreferenceQuote = checkBoxPreferenceArrayList.get(1);
Toast.makeText(MyPreferencesActivity.this,"size arraylist:"+checkBoxPreferenceArrayList.size(),Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MyPreferencesActivity.this,"listview is empty",Toast.LENGTH_SHORT).show();
}
}
public void uncheckAll(){
Log.d("testing","calling uncheckAll method");
checkBoxPreferenceEvent.setChecked(false);
checkBoxPreferenceQuote.setChecked(false);
}
public class MyPreferenceFragment extends PreferenceFragment{
CheckBoxPreference checkBoxPreferenceEvent;
CheckBoxPreference checkBoxPreferenceQuote;
ArrayList<CheckBoxPreference> checkBoxPreferenceArrayListInner = new ArrayList<>();
public MyPreferenceFragment(){
checkBoxPreferenceArrayListInner.add(checkBoxPreferenceEvent);
checkBoxPreferenceArrayListInner.add(checkBoxPreferenceQuote);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
//initialize checkbox preferences.
checkBoxPreferenceEvent = (CheckBoxPreference) findPreference("applicationUpdatesEvent");
checkBoxPreferenceQuote = (CheckBoxPreference) findPreference("applicationUpdatesQuote");
//listener for checkbox event
checkBoxPreferenceEvent.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
if(((CheckBoxPreference)preferenceScreen.findPreference("applicationUpdatesEvent")).isChecked()){
//initalize sharepreferences
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(ConfigBundle.SETTING, Context.MODE_PRIVATE);
//set value on shareprefenrence.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(ConfigBundle.SETTING_NOTIFICATION_EVENT,true);
editor.commit();
FirebaseMessaging.getInstance().subscribeToTopic("event");
Toast.makeText(getActivity(),"Event Notification Enable",Toast.LENGTH_LONG).show();
return true;
}else{
//initalize sharepreferences
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(ConfigBundle.SETTING, Context.MODE_PRIVATE);
//set value on shareprefenrence.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(ConfigBundle.SETTING_NOTIFICATION_EVENT,false);
editor.commit();
FirebaseMessaging.getInstance().unsubscribeFromTopic("event");
Toast.makeText(getActivity(),"Event Notification Disable",Toast.LENGTH_LONG).show();
return false;
}
}
});
//listener for checkbox quote
checkBoxPreferenceQuote.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
if(((CheckBoxPreference)preferenceScreen.findPreference("applicationUpdatesQuote")).isChecked()){
//initalize sharepreferences
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(ConfigBundle.SETTING, Context.MODE_PRIVATE);
//set value on shareprefenrence.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(ConfigBundle.SETTING_NOTIFICATION_QUOTE,true);
editor.commit();
FirebaseMessaging.getInstance().subscribeToTopic("quote");
Toast.makeText(getActivity(),"Quote Notification Enable",Toast.LENGTH_SHORT).show();
return true;
}else{
//initalize sharepreferences
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(ConfigBundle.SETTING, Context.MODE_PRIVATE);
//set value on shareprefenrence.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(ConfigBundle.SETTING_NOTIFICATION_QUOTE,false);
editor.commit();
FirebaseMessaging.getInstance().unsubscribeFromTopic("quote");
Toast.makeText(getActivity(),"Quote Notification Disable",Toast.LENGTH_SHORT).show();
return false;
}
}
});
}
//get arraylist that contain checkboxpreference
public ArrayList<CheckBoxPreference> getCheckBoxPreferenceArrayListInner(){
return checkBoxPreferenceArrayListInner;
}
}
}
HomeActivity.java
/*To make our option menu active,ovveride this method
so that we can add function fo each option menu that been created */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_1:
Intent intent = new Intent(HomeActivity.this,ContactActivity.class);
startActivity(intent);
break;
case R.id.menu_2:
Intent intent2 = new Intent(HomeActivity.this,MyPreferencesActivity.class);
startActivity(intent2);
break;
case R.id.menu_3:
//remove all information user from sharepreference.
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(ConfigBundle.SETTING, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(ConfigBundle.SETTING_FIRST_TIME,true);
editor.putString(ConfigBundle.SETTING_ID,"");
editor.putString(ConfigBundle.SETTING_EMAIL,"");
editor.putString(ConfigBundle.SETTING_USERNAME,"");
editor.putBoolean(ConfigBundle.SETTING_NOTIFICATION_EVENT,false);
editor.putBoolean(ConfigBundle.SETTING_NOTIFICATION_QUOTE,false);
editor.commit();
MyPreferencesActivity myPreferencesActivity = new MyPreferencesActivity();
myPreferencesActivity.uncheckAll();
FirebaseMessaging.getInstance().unsubscribeFromTopic("event");
FirebaseMessaging.getInstance().unsubscribeFromTopic("quote");
FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
//then go to main login activity content.
Intent intent3 = new Intent(HomeActivity.this,LoginActivity.class);
startActivity(intent3);
}
return super.onOptionsItemSelected(item);
}