I am Very new to Android Programming.. I have listed my code below. MainActivity.java
and activity_main.xml
. I have tried following solutions but to no avail.
- using only
this
- using
getapplicationcontext()
- using
getbasecontext()
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button text2 = (Button) findViewById(R.id.text2);
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
text2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_LONG).show();
}
}
}); // Disabling/Enabling Wifi Mechanism
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}