0

如果网络呼叫不可用,我正在尝试阻止应用程序崩溃。

如果网络调用可用,那么我想单击按钮继续进入下一个活动。渔获应显示敬酒。

但是应用程序仍然崩溃而不是显示吐司

这是我的代码:-

public void GoToStation(View v)
{

        try {

            Intent myIntent = new Intent(
                             MainActivity.this, CustomizedListViewStation.class);
            startActivityForResult(myIntent, 0);
        } catch (Exception myIntent) {
            // TODO Auto-generated catch block
            Context context = getApplicationContext();
            CharSequence text = "There is no data for this Community";
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

LOGCAT:-

08-01 12:36:56.944: E/AndroidRuntime(32556): FATAL EXCEPTION: main
08-01 12:36:56.944: E/AndroidRuntime(32556): java.lang.RuntimeException: Unable to start     activity     ComponentInfo{police.uk.greatermanchesterpoliceandroid/police.uk.greatermanchesterpoliceandroid.CustomizedListViewStation}: android.os.NetworkOnMainThreadException
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.os.Looper.loop(Looper.java:137)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at dalvik.system.NativeStart.main(Native Method)
08-01 12:36:56.944: E/AndroidRuntime(32556): Caused by: android.os.NetworkOnMainThreadException
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at police.uk.greatermanchesterpoliceandroid.XMLParser.getXmlFromUrl(XMLParser.java:45)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at police.uk.greatermanchesterpoliceandroid.CustomizedListViewStation.onCreate(CustomizedListViewStation.java:108)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.Activity.performCreate(Activity.java:5206)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-01 12:36:56.944: E/AndroidRuntime(32556):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-01 12:36:56.944: E/AndroidRuntime(32556):    ... 11 more
08-01 12:37:05.694: I/Process(32556): Sending signal. PID: 32556 SIG: 9
4

3 回答 3

1

如果您 200% 确定需要在主线程中进行网络活动,请使用此选项,请使用:

if (android.os.Build.VERSION.SDK_INT > 9) {
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);
}

否则,请查看ThreadsAsyncTasks

于 2013-08-01T11:01:09.967 回答
1

而不是先检查互联网连接。删除 try-catch 块并在 if 代码 -> 中调用 isOnline 函数if(isOnline()){}

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

用这个替换你的try-catch:

if (isOnline()){
    Intent myIntent = new Intent(MainActivity.this, CustomizedListViewStation.class);
    startActivityForResult(myIntent, 0);
} 

else {            
    Context context = getApplicationContext();
    CharSequence text = "There is no data for this Community";
    int duration = Toast.LENGTH_LONG;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}
于 2013-08-01T10:54:58.957 回答
0

这是因为您在主线程上运行代码,所以它抛出了一个NetworkOnMainThreadException. 这不在您的代码中,因此您无法捕获它,也不应该尝试捕获它,因为您的应用程序将永远无法工作,因为由于此错误,它永远不会尝试建立网络连接。

您需要在单独的线程(例如 ASyncTask)中执行网络活动,并且不会再抛出此异常。

此异常的原因是网络活动通常会导致应用程序延迟,因为网络调用往往会阻塞,直到它们完成。如果在主线程上执行网络活动,您的应用程序的用户可能会收到 ANR(应用程序无响应)消息,这意味着用户可以强制关闭您的应用程序,因为它看起来好像您的应用程序不再响应任何内容在后台它正在做网络活动。

于 2013-08-01T16:16:13.340 回答