簡體   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

我是Android編程的新手。我在下面列出了我的代碼。 MainActivity.javaactivity_main.xml 我嘗試了以下解決方案,但無濟於事。

  1. 僅使用this
  2. 使用getapplicationcontext()
  3. 使用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();

嘗試使用活動上下文而不是應用程序上下文。 請參閱此鏈接以獲得更詳盡的描述:

何時調用活動上下文或應用程序上下文?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM