【发布时间】:2011-08-08 19:34:06
【问题描述】:
好的,这是我的问题:
我将在没有互联网连接时开始新的活动,但新的活动屏幕是黑色的。新活动应显示 ImageView...
检查连接并开始新的活动:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
}
}
else {
startActivity(new Intent(main.this, no_connection.class));
}
NO_CONNECTION 活动:
package com.hello.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class no_connection extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connection_error);
ImageView image = (ImageView) findViewById(R.id.image_verkkovirhe);
}
}
这里是 CONNECTION_ERROR 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/connection_error"
android:fitsSystemWindows="true" android:orientation="vertical">
<ImageView
android:src="@drawable/verkkovirhe"
android:layout_width="fill_parent"
android:id="@+id/image_verkkovirhe"
android:layout_height="fill_parent"
android:clickable="false"
android:fitsSystemWindows="true"
android:visibility="visible"></ImageView>
</RelativeLayout>
或
也许我只能在没有网络连接时更改布局?当我尝试这个时,我会强制关闭?
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
}
}
else {
setContentView(R.layout.connection_error );
}
【问题讨论】:
-
您是否认为您有连接并且您没有设置其他内容视图(可能是空白的?)我也推荐这个
if (info != null) { Log.e("TAG", "info != null"); if (!info.isConnected()) { Log.e("TAG", "info.isconnted() !"); } } else { Log.e("TAG", "setContentView"); setContentView(R.layout.connection_error ); }用于记录目的以了解发生了什么 -
如果我更改我的代码,它会给我强制关闭?
-
FC 上的堆栈跟踪是什么?我在我的 Eclipse 中检查了该代码,并且没有丢失';'或“}”。
-
顺便说一句,您的布局不应该是您调用的 layout_id... 确保您的布局 xml 文件名为 connection_error.xml
标签: android android-layout android-activity connection android-manifest