根据我的 Internet 连接状态更改 setContentView 上的布局
Change Layout on setContentView according to my Internet Connection Status
我目前正在开发一个应用程序,我想在其中添加一项功能。当没有互联网时,activity 的布局必须改变,反之亦然。我在 android 方面不太擅长,我将这段代码用于我的小想法。任何想法如何实现这一目标。当我尝试应用程序崩溃时。
public class RescuingActivity extends AppCompatActivity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ConnectivityStatus.isConnected(getContext())){
setContentView(R.layout.rescue_page);
}else {
setContentView(R.layout.no_connection);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getContext().registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
Logcat 错误:
xxx/E/AndroidRuntime: FATAL EXCEPTION: main
Process: xxx, PID: 6258
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.xxxxxxx/xxx.xxx.xxxxxxx.view.RescuingActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kit.arescue.view.RescuingActivity.onCreate(RescuingActivity.java:90)
试试这个功能
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) Login_Page.this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
try
{
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(500); //choose your own timeframe
urlc.setReadTimeout(500); //choose your own timeframe
urlc.connect();
int networkcode2 = urlc.getResponseCode();
return (urlc.getResponseCode() == 200);
} catch (IOException e)
{
return (false); //connectivity exists, but no internet.
}
}
}
return false;
}
并且不要忘记添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
找到答案,尝试使用 2 个视图。
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(!ConnectivityStatus.isConnected(getContext())){
childLayoutRescuingActivity.setVisibility(INVISIBLE);
childLayoutNoConnection.setVisibility(View.VISIBLE);
}else {
childLayoutRescuingActivity.setVisibility(View.VISIBLE);
childLayoutNoConnection.setVisibility(INVISIBLE);
}
}
};
我目前正在开发一个应用程序,我想在其中添加一项功能。当没有互联网时,activity 的布局必须改变,反之亦然。我在 android 方面不太擅长,我将这段代码用于我的小想法。任何想法如何实现这一目标。当我尝试应用程序崩溃时。
public class RescuingActivity extends AppCompatActivity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ConnectivityStatus.isConnected(getContext())){
setContentView(R.layout.rescue_page);
}else {
setContentView(R.layout.no_connection);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getContext().registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
Logcat 错误:
xxx/E/AndroidRuntime: FATAL EXCEPTION: main Process: xxx, PID: 6258 java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.xxxxxxx/xxx.xxx.xxxxxxx.view.RescuingActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280) at android.app.ActivityThread.access0(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5059) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.kit.arescue.view.RescuingActivity.onCreate(RescuingActivity.java:90)
试试这个功能
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) Login_Page.this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
try
{
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(500); //choose your own timeframe
urlc.setReadTimeout(500); //choose your own timeframe
urlc.connect();
int networkcode2 = urlc.getResponseCode();
return (urlc.getResponseCode() == 200);
} catch (IOException e)
{
return (false); //connectivity exists, but no internet.
}
}
}
return false;
}
并且不要忘记添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
找到答案,尝试使用 2 个视图。
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(!ConnectivityStatus.isConnected(getContext())){
childLayoutRescuingActivity.setVisibility(INVISIBLE);
childLayoutNoConnection.setVisibility(View.VISIBLE);
}else {
childLayoutRescuingActivity.setVisibility(View.VISIBLE);
childLayoutNoConnection.setVisibility(INVISIBLE);
}
}
};