简体   繁体   English

Toast未在Android App中显示,但首先显示了Toast,然后在两天后停止显示

[英]Toast Is not being Displayed in The Android App , But first it was being diplayed then after two days it stopped

I am Very new to Android Programming.. I have listed my code below. 我是Android编程的新手。我在下面列出了我的代码。 MainActivity.java and activity_main.xml . MainActivity.javaactivity_main.xml I have tried following solutions but to no avail. 我尝试了以下解决方案,但无济于事。

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

}
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? 何时调用活动上下文或应用程序上下文?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM