2014-07-03 28 views
0

我有一個關於CheckBoxPreferences的問題。我已經閱讀了相關線索的數量並嘗試了其中的一些,但由於我是初學者,我仍然在努力尋找解決方案來解決我面臨的問題。下面的代碼是在谷歌地圖,並在總體上確實把它應用多邊形:只有在活動重新啓動後,CheckBoxPreferences的值纔會生效

public class ZurichGreifensee extends Activity implements OnClickListener { 

    String latLngUster = "lat=47.348491&lon=8.717529"; 

    private final LatLng LOCATION_GREIFENSEE = new LatLng(47.348124, 8.681688); 

    private GoogleMap mMap; 
    Button fishActivity; 
    ToggleButton mapView; 
    Polygon Naturschutzgebiet, Seeschutzzone; 
    CheckBoxPreference naturschutzgebiet; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.lakes_info); 

     NetworkConnectivityCheck checkit = new NetworkConnectivityCheck(); 
     Boolean conn = checkit.checkNow(this.getApplicationContext()); 
     if(conn == true){ 

     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     CameraUpdate updateZurichGreifensee = CameraUpdateFactory.newLatLngZoom(LOCATION_GREIFENSEE, 13); 
     mMap.animateCamera(updateZurichGreifensee); 
     mMap.setMyLocationEnabled(true); 

     mapView = (ToggleButton) findViewById(R.id.tbSatNorMapView); 
     fishActivity = (Button) findViewById(R.id.bWhereAmI); 
     mapView.setOnClickListener(this); 
     fishActivity.setOnClickListener(this); 

     }else{ 
      connectivityMessage("Kein Netzwerk"); 
     } 

     SharedPreferences getGewasserRevierKarte = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     boolean naturschutzgebiet = getGewasserRevierKarte.getBoolean("naturschutzgebiet", false);{ 
      if (naturschutzgebiet == true){ 
       addNaturschutzgebiet();} 
     } 

     boolean seeschutzzone_1 = getGewasserRevierKarte.getBoolean("seeschutzzone_1", false);{ 
      if (seeschutzzone_1 == true) 
       addSeeschutzzone(); 
     } 
    } 

    private void connectivityMessage(String string) { 
     // TODO Auto-generated method stub 
     Context context = getApplicationContext(); 
     Toast toast = Toast.makeText(context, "", Toast.LENGTH_LONG); 
     toast.setGravity(Gravity.CENTER, 0, 0); 
     toast.setText(string); 
     toast.show(); 
    } 


    private void addNaturschutzgebiet() { 
     Naturschutzgebiet = mMap.addPolygon(new PolygonOptions() 
      .add(new LatLng(47.366676,8.673754), new LatLng(47.367526,8.67236), new LatLng(47.369459,8.670965), new LatLng(47.371886,8.669538), 
       new LatLng(47.372525,8.669184), new LatLng(47.375453,8.661481), new LatLng(47.376819,8.657801), new LatLng(47.378032,8.652318), 
       new LatLng(47.375962,8.651159), new LatLng(47.378839,8.646846), new LatLng(47.378759,8.646739), new LatLng(47.375562,8.651449), 
       new LatLng(47.37432,8.650172), new LatLng(47.37291,8.652007), new LatLng(47.371639,8.650355), new LatLng(47.370403,8.650634), 
       new LatLng(47.368972,8.650859), new LatLng(47.368071,8.652973), new LatLng(47.368173,8.653337), new LatLng(47.368122,8.653788), 
       new LatLng(47.367904,8.654228), new LatLng(47.367947,8.654593), new LatLng(47.369829,8.653616), new LatLng(47.371057,8.653488), 
       new LatLng(47.372155,8.65588), new LatLng(47.372656,8.657597), new LatLng(47.373608,8.661545), new LatLng(47.37262,8.663927), 
       new LatLng(47.371145,8.666051), new LatLng(47.370098,8.667457), new LatLng(47.370055,8.668186), new LatLng(47.369386,8.66839), 
       new LatLng(47.369241,8.669044), new LatLng(47.367497,8.67074), new LatLng(47.367243,8.670557), new LatLng(47.367293,8.670836), 
       new LatLng(47.366792,8.671544), new LatLng(47.366836,8.671823), new LatLng(47.365935,8.672746)) 
        .strokeWidth(2) 
        .strokeColor(Color.RED) 
        .fillColor(Color.argb(64, 255, 0, 0))); 
     } 

    private void addSeeschutzzone() { 
     Seeschutzzone = mMap.addPolygon(new PolygonOptions() 
      .add(new LatLng(47.367947,8.654593), new LatLng(47.369829,8.653616), new LatLng(47.371057,8.653488), 
       new LatLng(47.372155,8.65588), new LatLng(47.372656,8.657597), new LatLng(47.373608,8.661545), new LatLng(47.37262,8.663927), 
       new LatLng(47.371145,8.666051), new LatLng(47.370098,8.667457), new LatLng(47.370055,8.668186), new LatLng(47.369386,8.66839), new LatLng(47.369241,8.669044), 
       new LatLng(47.368187,8.670085), new LatLng(47.368412,8.669001), new LatLng(47.36964,8.667097), new LatLng(47.370715,8.665166), 
       new LatLng(47.371442,8.663599), new LatLng(47.371413,8.661239), new LatLng(47.371006,8.658149), new LatLng(47.369989,8.656261)) 
        .strokeWidth(2) 
        .strokeColor(Color.MAGENTA) 
        .fillColor(Color.argb(64, 170, 0, 120))); 
    } 



    @Override 
    public void onClick(View arg0) { 
     switch (arg0.getId()) { 
       case R.id.bWhereAmI: 
        String locationZurichGreifensee = latLngUster.toString(); 
        Bundle basketZurich = new Bundle(); 
        basketZurich.putString("location", locationZurichGreifensee); 
        Intent t = new Intent(ZurichGreifensee.this, MainActivity.class); 
        t.putExtras(basketZurich); 
        startActivity(t); 
        break; 
       } 

       //Switching between normal and hybrid view 
       if (mapView.isChecked()) { 
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
       }else { 
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       } 
     } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // TODO Auto-generated method stub 
     super.onCreateOptionsMenu(menu); 
     MenuInflater gewasserMenu = getMenuInflater(); 
     gewasserMenu.inflate(R.menu.gewasserkarte, menu); 
     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()){ 
     case R.id.gewasserkarte: 
      Intent gewasser_revier_karte = new Intent(ZurichGreifensee.this, GewasserRevierKarte.class); 
      startActivity(gewasser_revier_karte); 
      break; 
     } 
     return false; 
    } 
} 

,這是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <CheckBoxPreference 
     android:key="naturschutzgebiet" 
     android:title="Naturschutzgebiet" 
     android:summaryOn="Wege nicht verlassen, Hunde an der Leine führen" 
     android:icon="@drawable/naturschutzgebiet" 
     android:defaultValue="false" /> 

    <CheckBoxPreference 
     android:key="seeschutzzone_1" 
     android:title="Seeschutzzone" 
     android:summaryOn="Befahren mit Booten oder anderen Schwimmkörpern sowie Baden ganzjährig verboten" 
     android:icon="@drawable/seeschutzzone_1" 
     android:defaultValue="false" /> 

</PreferenceScreen> 

爲了讓地圖上顯示的定義多邊形上面的一個,在首選項菜單中勾選相應的複選框。現在,即使我這樣做,在關閉首選項菜單後,多邊形也不可見。相反,我必須重新啓動ZurichGreifensee活動。只有這樣代碼才能在地圖上應用多邊形。

我的問題是,我需要在上面的代碼中進行更改/添加,以便在我勾選所需偏好複選框並關閉/退出首選項菜單後立即顯示多邊形?

非常感謝提前。

回答

0

你需要重寫的onResume方法,並檢查用戶是否已經按下複選框有

@Override 
public void onResume() { 
    super.onResume(); 
    //if user has checked the box 
    //is rectangle isn't already drawn 
    //draw rectangle 
} 
+0

不客氣。你試過了嗎?它是以這種方式工作嗎? – DDsix

+0

@Override \t保護無效的onResume(){ \t \t // TODO自動生成方法存根 \t \t super.onResume(); \t \t if(naturschutzgebiet.isChecked()&&(Naturschutzgebiet == null)); \t \t addNaturschutzgebiet(); \t} below onCreate方法拋出一個NullPointerException異常 放置是否正確? – Budjoni

相關問題