0

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.

  1. using only this
  2. using getapplicationcontext()
  3. 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);
}

}
codeMagic
  • 44,549
  • 13
  • 77
  • 93

1 Answers1

0
Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_LONG).show();

Try using the activity context rather than the application context. See this link for a more thorough description:

When to call activity context OR application context?

Community
  • 1
  • 1
Razz
  • 220
  • 1
  • 6