如果不在这里工作,为什么呢?

时间:2016-05-18 10:01:30

标签: java android if-statement

当我获得特定地点的内容名称时,我的地图将集中在那个地方,但它只适用于一个地方,为什么会这样?这是我的代码

 protected void onResume() {
    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(1000);
            } catch (InterruptedException e){
                e.printStackTrace();
            } finally {

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = getIntent();
                        String restaurant_name = intent.getStringExtra("restaurant_name");
                        //Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();

                        if(restaurant_name != null ) {
                            if (restaurant_name.equals("Fun N Food")) {
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(26.89209, 75.82759))
                                        .title("FUN N FOOD"))
                                        .showInfoWindow();


                            }
                            else
                            if (restaurant_name.equals("Hoagies")) {
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89515, 75.83052), 15.0f));
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(26.89515, 75.83052))
                                        .title("HOAGIES"))
                                        .showInfoWindow();


                            }
                            else
                            if (restaurant_name.equals("Ping Pang")) {
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89568, 75.83060), 15.0f));
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(26.89568, 75.83060))
                                        .title("PING PANG"))
                                        .showInfoWindow();


                            }


                        }else {
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //Toast.makeText(MainActivity.this, "It was not", Toast.LENGTH_LONG).show();
                                }
                            });

                        }
                    }
                });
   /* Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show(); */

            }
        }
    };
    timer.start();

    super.onResume();

}

这里只有Fun n Food正在运作。为什么所有其他两个人都没有工作呢?我也尝试切换机箱,但没有一个工作。

3 个答案:

答案 0 :(得分:1)

错误在于:else

                `if` 
             it's actually like else is a single statement and if is a separate statement. So just remove the spaces and have `else if` 

替换为以下内容:

protected void onResume() {
    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(1000);
            } catch (InterruptedException e){
                e.printStackTrace();
            } finally {

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = getIntent();
                        String restaurant_name = intent.getStringExtra("restaurant_name");
                        //Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();

                        if(restaurant_name != null ) {
                            if (restaurant_name.equals("Fun N Food")) {
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(26.89209, 75.82759))
                                        .title("FUN N FOOD"))
                                        .showInfoWindow();


                            }
                            else if (restaurant_name.equals("Hoagies")) {
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89515, 75.83052), 15.0f));
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(26.89515, 75.83052))
                                        .title("HOAGIES"))
                                        .showInfoWindow();


                            }
                            else if (restaurant_name.equals("Ping Pang")) {
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89568, 75.83060), 15.0f));
                                mMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(26.89568, 75.83060))
                                        .title("PING PANG"))
                                        .showInfoWindow();


                            }


                        }else {
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //Toast.makeText(MainActivity.this, "It was not", Toast.LENGTH_LONG).show();
                                }
                            });

                        }
                    }
                });
   /* Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show(); */

            }
        }
    };
    timer.start();

    super.onResume();

}

答案 1 :(得分:0)

Buddy,遵循 if else 条件的结构,如

if(..)
{
   //todo
}
else
{
   if(..)
   {
       //todo
   }
   else
   {
      //todo
   }
}

if(..)
{
   //todo
}
else if(..)
{
   //todo
}
else
{
  //todo
}

答案 2 :(得分:0)

if(restaurant_name != null ) {
                        if (restaurant_name.equals("Fun N Food")) {
                            //Todo
                        }
                        else if (restaurant_name.equals("Hoagies")) {
                            //Todo
                        }
                        else if (restaurant_name.equals("Ping Pang")) {     
                            //Todo                           
                        }
}