我试图得到一个Switch
小部件的状态。为此,我创建了一个新的变量:Switch notifSwitch = findViewById(R.id.notif_switch)
。
然而这不起作用,因为现在notifSwitch
是null
和铸造setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
得到我java.lang.NullPointerException
。
我怀疑这与以下事实有关:具有'notif_switch
'id的小部件(我试图获得值的Switch
)不是(直接)在activity_main.xml
中,这是我传递给setContentView
的布局我的onCreate
方法的开头,因为如果我在setContentView(R.layout.line_notif);
declaration之前添加Switch
,代码工作正常。该应用程序只包含在line_notif.xml
layout中的内容,它只是Switch
和TextView
,但至少我没有任何错误,我能够获得Switch
小部件的状态。
我不知道如何从我的MainActivity.java
file访问小部件,但无法找到这样做的方法。在创建自己的帖子之前,我尝试了尽可能多的帖子。我很抱歉,如果我错过了一个回答我的问题,但我找不到解决这个问题的方法,我真的很感激帮助。
谢谢。
MainActivity.java
:
...
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private MainActivity activity;
private boolean notificationsOn=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activity = this;
...
// HERE IS THE PROBLEM
Switch notifSwitch = findViewById(R.id.notif_switch); // notifSwitch = null
// Error on next line
notifSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
notificationsOn = true;
Toast.makeText(getApplicationContext(), "Notifications enabled", Toast.LENGTH_SHORT).show();
} else {
notificationsOn = false;
Toast.makeText(getApplicationContext(), "Notifications disabled", Toast.LENGTH_SHORT).show();
}
}
});
}
...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/colorPrimary"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
android:background="@color/colorPrimary"/>
</android.support.v4.widget.DrawerLayout>
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
...>
...
<item
android:id="@+id/nav_notifications"
app:actionLayout="@layout/line_notif"
android:enabled="true"
tools:ignore="MenuTitle" />
...
</menu>
line_notif.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/line_notif"
android:background="@android:color/transparent">
...
<Switch
android:id="@+id/notif_switch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="end"
android:theme="@style/SwitchTheme"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingTop="15dp"/>
</RelativeLayout>
完整错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectname/com.example.projectname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
我怀疑这与以下事实有关:具有'
notif_switch
'id的小部件(我试图获得值的Switch
)不是(直接)在activity_main.xml
中,这是我传递给setContentView
的布局我的onCreate
方法的开头
你是对的,这就是问题所在。我建议你阅读如何创建小部件的this post。您将了解到窗口小部件中允许的组件是有限的,因此您与它们的交互也是如此。