mercredi 20 août 2014

Switch (Button) throws NullPointerException


Vote count:

0




I know this question already exists, but I tried serveral solutions and none worked for me.


In my case, I need a Switch Button in the ActionBar.



  • I tried to manually add a minSdk Verision

  • I tried to use the id of the item in the defined xml


Here is some of my code:


mainactivity:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_minepedia_main);

switchButton = (Switch) findViewById(R.id.switch_button);

//The following line throws an exception, because switchButton
//is null, even though I tried to take it on the upper line.
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

//The following is a local boolean variable
if (isChecked) {
ONLINE_MODE_ENABLED = true;
} else {
ONLINE_MODE_ENABLED = false;
}

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; --> main_actions.xml
getMenuInflater().inflate(R.menu.main_actions, menu);
return super.onCreateOptionsMenu(menu);
}


mainactivity_layout:



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Minepedia_main">

<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/items_button"
android:onClick="startItemsMenu" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about_button"
android:onClick="startAboutScreen" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="50dp"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/update_offline_button"
android:onClick="startOfflineUpdate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/offline_sync_info"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>


main_actions.xml (menu):



<menu xmlns:android="http://ift.tt/nIICcg"
xmlns:yourapp="http://ift.tt/GEGVYd" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/switch_layout"
/>
</menu>


switch_layout.xml:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
android:textOff="Offline"
android:textOn="Online"/>
</RelativeLayout>


asked 46 secs ago







Switch (Button) throws NullPointerException

Aucun commentaire:

Enregistrer un commentaire