Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Buttons not being displayed?
    primarykey
    data
    text
    <p>I am using the code below and I have a button that is not being displayed. I think it has something to do with "SetContentView" because if I remove one of them the button will show up. I am not sure how to get around this so that everything shows up? Thanks!</p> <pre><code>import java.util.List; import android.app.Activity; import android.os.Bundle; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TestActivity extends Activity { /** Called when the activity is first created. * @return */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button OffWifi = (Button)findViewById(R.id.offwifi); OffWifi.setOnClickListener(new OnClickListener() { public void onClick(View v) { WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(false); } }); TextView tv = new TextView(this); TextView status = new TextView(this); WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wc = new WifiConfiguration(); wc.SSID = "\"Test\""; //IMP! This should be in Quotes!! wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.priority = 10; wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.preSharedKey = "\"Password\""; WifiManager wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE); boolean res1 = wifiManag.setWifiEnabled(true); int res = wifi.addNetwork(checkPreviousConfiguration(wc)); Log.d("WifiPreference", "add Network returned " + res ); boolean es = wifi.saveConfiguration(); Log.d("WifiPreference", "saveConfiguration returned " + es ); boolean b = wifi.enableNetwork(res, true); Log.d("WifiPreference", "enableNetwork returned " + b ); tv.setText("You are now connected! " + "Version 1.1"); status.setText("The was an error connecting, please try again."); //@Override try { Thread.sleep(5000); ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if (connec != null &amp;&amp; (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ //You are connected, do something online. setContentView(tv); }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) { //Not connected. setContentView(status); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); List&lt;WifiConfiguration&gt; configs = wifi.getConfiguredNetworks(); for(WifiConfiguration config : configs) { if(config.SSID.equals(wc.SSID)) return config; } return wc; } } </code></pre> <p>Main XML File </p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/tv" android:layout_width="246dp" android:layout_height="wrap_content" /&gt; &lt;Button android:id="@+id/offwifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Turn Wifi Off" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload