我有一个Android应用程序,如果用户选中了启用通知功能的滑块,则每天都会通过NotificationActivity发送一次通知 我的问题很简单:从MainActivity启动NotificationActivity时,应用程序崩溃
我有一个Android应用程序,如果用户选中了启用通知功能的滑块,则每天都会通过NotificationActivity发送一次通知 我的问题很简单:从MainActivity启动NotificationActivity时,应用崩溃 日志告诉我,这些视图来自提供nullPointerException的视图:
我认为NotificationActivity中的视图(editText,滑块,复选框)未正确绑定
错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matt.android.mynews/com.matt.android.mynews.controllers.activities.NotificationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setSelection(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setSelection(int)' on a null object reference
at com.matt.android.mynews.controllers.activities.NotificationActivity.initNotification(NotificationActivity.java:109)
at com.matt.android.mynews.controllers.activities.NotificationActivity.configureDisplayAndPrefs(NotificationActivity.java:185)
at com.matt.android.mynews.controllers.activities.NotificationActivity.onCreate(NotificationActivity.java:63)
at android.app.Activity.performCreate(Activity.java:6860)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2674)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521)
提供NullPointerException的setSelection(0)用于EditText自动打开键盘 如果我将其删除,则异常来自下一个小部件:复选框等
NotificationActivity:
public class NotificationActivity extends AppCompatActivity {
SharedPreferencesManager preferences;
@BindView(R.id.search_query)
EditText search_query;
@BindView(R.id.switch_notification)
Switch switchNotification;
@BindView(R.id.travel_check_box)
CheckBox travelCheckBox;
@BindView(R.id.sports_check_box)
CheckBox sportsCheckBox;
@BindView(R.id.arts_check_box)
CheckBox artsCheckBox;
@BindView(R.id.politics_check_box)
CheckBox politicsCheckBox;
@BindView(R.id.entrepreneurs_check_box)
CheckBox entrepreneursCheckBox;
@BindView(R.id.business_check_box)
CheckBox businessCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
this.configureDisplayAndPrefs();
}
/**
* Get and set toolbar
*/
private void configureToolBar() {
Toolbar toolbar = findViewById(R.id.toolbar_main_activity);
setSupportActionBar(toolbar);
//This is for back button
//Get a support Action bar corresponding to above toolbar
ActionBar actionBar = getSupportActionBar();
//Enable the up button
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
/**
* Check if the switch has been activated by the user and if so, get the last user input
*/
private void isSwitchChecked(){
boolean switchIsChecked = preferences.getBoolean(PREF_KEY_SWITCH);
if (switchIsChecked) {
//Set switch to checked
switchNotification.setChecked(true);
//Set search query
search_query.setText(preferences.getString(PREF_KEY_QUERY));
//Check the checkboxes that were selected
artsCheckBox.setChecked(preferences.getBoolean(PREF_KEY_ARTS));
politicsCheckBox.setChecked(preferences.getBoolean(PREF_KEY_POLITICS));
entrepreneursCheckBox.setChecked(preferences.getBoolean(PREF_KEY_ENTREPRENEURS));
sportsCheckBox.setChecked(preferences.getBoolean(PREF_KEY_SPORTS));
travelCheckBox.setChecked(preferences.getBoolean(PREF_KEY_TRAVEL));
businessCheckBox.setChecked(preferences.getBoolean(PREF_KEY_BUSINESS));
}
}
/**
* Set listener to switch to enable notifications
*/
private void initNotification() {
//Open the keyboard automatically
search_query.setSelection(0);
Objects.requireNonNull(this.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//Set Listener to switch
switchNotification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//Get user input
preferences.getUserInput(search_query, null, null,
artsCheckBox, businessCheckBox, entrepreneursCheckBox,
politicsCheckBox, sportsCheckBox, travelCheckBox);
//If at least 1 checkbox is selected and user has put one search query --> enable notifications
if(preferences.checkConditions()) {
saveNotificationUrlAndState();
enableNotification();
} else {
preferences.clearInput();
Toast.makeText(getApplicationContext(), "Please select at least a categorie and a keyword", Toast.LENGTH_SHORT).show();
switchNotification.toggle();
}
}
//If switch is unchecked
else {
cancelNotification();
}
}
});
}
/**
* Get the user input and save it in SharedPreferences
*/
private void saveNotificationUrlAndState() {
//Switch button
preferences.putBoolean(PREF_KEY_SWITCH, true);
//Edit hint text Text Query
preferences.putString(PREF_KEY_QUERY, search_query.getText().toString());
//CheckBoxes
preferences.putBoolean(PREF_KEY_ARTS, artsCheckBox.isChecked());
preferences.putBoolean(PREF_KEY_POLITICS, politicsCheckBox.isChecked());
preferences.putBoolean(PREF_KEY_BUSINESS, businessCheckBox.isChecked());
preferences.putBoolean(PREF_KEY_ENTREPRENEURS, entrepreneursCheckBox.isChecked());
preferences.putBoolean(PREF_KEY_SPORTS, sportsCheckBox.isChecked());
preferences.putBoolean(PREF_KEY_TRAVEL, travelCheckBox.isChecked());
//Save search url
preferences.createSearchUrlForAPIRequest();
preferences.saveUrl(PREF_KEY_NOTIFICATION_URL);
}
/**
* Use work manager to run a notification
*/
private void enableNotification(){
}
/**
* Cancel notification switch
*/
private void cancelNotification(){
preferences.clearInput();
preferences.putBoolean(PREF_KEY_SWITCH, false);
//Worker.cancel
Toast.makeText(NotificationActivity.this, "Notifaction disabled", Toast.LENGTH_SHORT).show();
}
private void configureDisplayAndPrefs() {
this.configureToolBar();
preferences = new SharedPreferencesManager(this);
this.isSwitchChecked();
this.initNotification();
}
}
答案 0 :(得分:1)
将活动更改为
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
ButterKnife.bind(this);
}
您忘了绑定视图。
答案 1 :(得分:1)
在活动的Oncreate()中看不到基本的黄油刀绑定代码
ButterKnife.bind(this);
看起来视图未正确绑定