如果网络呼叫不可用,我正在尝试阻止应用程序崩溃。
如果网络调用可用,那么我想单击按钮继续进入下一个活动。渔获应显示敬酒。
但是应用程序仍然崩溃而不是显示吐司
这是我的代码:-
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