1
好的,這裏是我的問題:如何在沒有網絡連接時調用新的佈局?
我要開始新的活動,當沒有互聯網連接,但新的活動屏幕是黑色的。新的活動應該表現出的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>
OR
也許我只能在沒有網絡連接的情況下更改佈局?當我嘗試這個時,我會接近力量?
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
}
}
else {
setContentView(R.layout.connection_error);
}
你有沒有認爲你有連接,並且不設置其他內容視圖 我也建議此 '如果(資訊=(可能是空白的?)! null){ Log.e(「TAG」,「info!= null」);如果(!info.isConnected())Log.e(「TAG」,「info.isconnted()!」); } } else { Log.e(「TAG」,「setContentView」); setContentView(R.layout.connection_error); }' 爲了記錄目的來弄清楚發生了什麼 – Mutmatt
如果我改變我的代碼,它使我強制關閉? – Eljas
FC上的堆棧跟蹤是什麼?我在日食中檢查了代碼,並且沒有丟失';'要麼 '}'。 – Mutmatt