Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用try-and-catch显示AlertDialog_Java_Android_Android Studio_Try Catch - Fatal编程技术网

Java 使用try-and-catch显示AlertDialog

Java 使用try-and-catch显示AlertDialog,java,android,android-studio,try-catch,Java,Android,Android Studio,Try Catch,我想检查我何时连接到服务器,为此,我正在使用在internet上找到的代码,我不知道如何实现它,不连接时如何显示警报对话框或任何类型的警告。任何帮助都将不胜感激 代码: 日志: FATAL EXCEPTION: main Process: com.example.lupitagarcia.yosoyvallarta, PID: 1

我想检查我何时连接到服务器,为此,我正在使用在internet上找到的代码,我不知道如何实现它,不连接时如何显示警报对话框或任何类型的警告。任何帮助都将不胜感激

代码:

日志:

FATAL EXCEPTION: main
                                                                                    Process: com.example.lupitagarcia.yosoyvallarta, PID: 11573
                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                                                                                        at com.example.lupitagarcia.yosoyvallarta.Reportes.onMapReady(Reportes.java:195)
                                                                                        at com.google.android.gms.maps.MapView$zza$1.zza(Unknown Source)
                                                                                        at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                                                                                        at android.os.Binder.transact(Binder.java:387)
                                                                                        at er.b(:com.google.android.gms.DynamiteModulesB@11951434:20)
                                                                                        at com.google.android.gms.maps.internal.bf.a(:com.google.android.gms.DynamiteModulesB@11951434:5)
                                                                                        at com.google.maps.api.android.lib6.impl.bc.run(:com.google.android.gms.DynamiteModulesB@11951434:5)
                                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7406)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是它使用PHP从数据库检索信息的代码,这里也是catlog发送给我的代码 代码:

ArrayList位置=null;
字符串url=”http://app/get.php";
试一试{
JSONArray数据=新的JSONArray(getHttpGet(url));
位置=新的ArrayList();
HashMap图;
对于(int i=0;i
试试这个:

if (isConnectedToServer("http://yourserver.com", 60)) {
   new AlertDialog.Builder(this)
      .setTitle("Connected")
      .setMessage("You are connected to server")
      .create().show();
} else {
   new AlertDialog.Builder(this)
      .setTitle("Not Connected")
      .setMessage("You are not connected to server")
      .create().show();
}
更新:

    ArrayList<HashMap<String, String>> location = new ArrayList<HashMap<String, String>>(); //do your instance creation here to avoid nullpointer if your try-catch go to catch.
    String url = "http://app/get.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));
ArrayList location=new ArrayList();//在此处创建实例,以避免在try-catch-go-to-catch时出现空指针。
字符串url=”http://app/get.php";
试一试{
JSONArray数据=新的JSONArray(getHttpGet(url));

使用此选项,它仅在连接时显示消息,但在没有连接时应用程序停止。您可以显示日志您遇到了什么错误吗?似乎您在else语句上执行了一些操作,而不是显示对话框。错误指向您的ArrayList,该列表为空。您可以显示关于如何执行的代码吗基本上,这是在应用程序上要做的第一件事,然后有一个数组,它从数据库中检索一些信息,如姓名、地址等。让我更新帖子。在声明数组后尝试初始化它:
ArrayList youraylist=new arrarylist()
if (isConnectedToServer("http://yourserver.com", 60)) {
   new AlertDialog.Builder(this)
      .setTitle("Connected")
      .setMessage("You are connected to server")
      .create().show();
} else {
   new AlertDialog.Builder(this)
      .setTitle("Not Connected")
      .setMessage("You are not connected to server")
      .create().show();
}
    ArrayList<HashMap<String, String>> location = new ArrayList<HashMap<String, String>>(); //do your instance creation here to avoid nullpointer if your try-catch go to catch.
    String url = "http://app/get.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));
boolean isConnected = isConnectedToServer("Your server", 10);

if(isConnected){
                    new AlertDialog.Builder(this)
                    .setMessage("You are Successfully Connected"
                            + getString(R.string.app_name))
                    .setPositiveButton("OK",
                         new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface 
                                             dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
}
else{
                    new AlertDialog.Builder(this)
                    .setMessage("Something Went Wrong!!"
                            + getString(R.string.app_name))
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface 
                                            dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
}