gpt4 book ai didi

Android 检查网络连接选项卡加载程序

转载 作者:行者123 更新时间:2023-11-29 15:25:55 25 4
gpt4 key购买 nike

我想在加载选项卡之前实现检查网络连接。任何人都可以告诉我它是如何完成的吗?以下是我主要 Activity 的代码。我仍然无法弄清楚它是如何完成的。请帮我。

谢谢

public class MainActivity extends SherlockFragmentActivity {
Tab tab;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
if (!isNetworkAvailable()) {
Toast.makeText(getApplicationContext(),
"Internet Connection is Not Available", 10).show();
finish();
} else {

ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

tab = actionBar.newTab().setText("New Games")
.setTabListener(new NewFragment())
.setIcon(R.drawable.android);
actionBar.addTab(tab);

tab = actionBar.newTab().setText("TOP Games")
.setTabListener(new TopFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);

tab = actionBar.newTab().setText("BEST Games")
.setTabListener(new BestFragment())
.setIcon(R.drawable.reader);
actionBar.addTab(tab);

}

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}

}

洛卡特:

11-01 21:55:35.023: E/AndroidRuntime(24710): FATAL EXCEPTION: main
11-01 21:55:35.023: E/AndroidRuntime(24710): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.ClassNotFoundException: in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.actionbarsherlocknavtab-1.apk]
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.os.Looper.loop(Looper.java:130)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.ActivityThread.main(ActivityThread.java:3691)
11-01 21:55:35.023: E/AndroidRuntime(24710): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 21:55:35.023: E/AndroidRuntime(24710): at java.lang.reflect.Method.invoke(Method.java:507)
11-01 21:55:35.023: E/AndroidRuntime(24710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 21:55:35.023: E/AndroidRuntime(24710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 21:55:35.023: E/AndroidRuntime(24710): at dalvik.system.NativeStart.main(Native Method)
11-01 21:55:35.023: E/AndroidRuntime(24710): Caused by: java.lang.ClassNotFoundException: in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.actionbarsherlocknavtab-1.apk]
11-01 21:55:35.023: E/AndroidRuntime(24710): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-01 21:55:35.023: E/AndroidRuntime(24710): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-01 21:55:35.023: E/AndroidRuntime(24710): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-01 21:55:35.023: E/AndroidRuntime(24710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
11-01 21:55:35.023: E/AndroidRuntime(24710): ... 11 more

最佳答案

在您的 mainActivity Oncreate 方法中写下这段代码。

if (!isNetworkAvailable()) {
Toast.makeText(getApplicationContext(),
"Internet Connection is Not Available", 10).show();
finish();
} else {

ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

tab = actionBar.newTab().setText("New Games")
.setTabListener(new NewFragment())
.setIcon(R.drawable.android);
actionBar.addTab(tab);

tab = actionBar.newTab().setText("TOP Games")
.setTabListener(new TopFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);

tab = actionBar.newTab().setText("BEST Games")
.setTabListener(new BestFragment())
.setIcon(R.drawable.reader);
actionBar.addTab(tab);

}

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}

关于Android 检查网络连接选项卡加载程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13177518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com