2013-07-01 73 views
1

我正在將應用程序切換到android,並且遇到了碎片問題。RelativeLayout中的Android碎片崩潰應用程序

當我沒有RelativeLayout,只是Fragment它工作正常,但隨着佈局(相對或線性)的介紹它崩潰。我試圖在地圖頂部添加一些按鈕,最好是疊加而不是放在自己的空間中。

event_layout.xml:

<RelativeLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/relativeLayout"> 
    <fragment 
     android:id="@+id/the_map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment"/> 
    <Button android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:text="Start service"/> 
</RelativeLayout> 

MainActivity.java:

public class MainActivity extends FragmentActivity { 
    // Fragment TabHost as mTabHost 
    private FragmentTabHost mTabHost; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // set up the tabhost 
     mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); 
      mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 
      mTabHost.addTab(mTabHost.newTabSpec("event").setIndicator("Event Locations", 
        getResources().getDrawable(R.drawable.ic_event_tab)), 
        EventFragment.class, null); 
      mTabHost.addTab(mTabHost.newTabSpec("itin").setIndicator("Itinerary", 
        getResources().getDrawable(R.drawable.ic_itin_tab)), 
         ItineraryFragment.class, null); 
      mTabHost.addTab(mTabHost.newTabSpec("info").setIndicator("Kendal Info", 
        getResources().getDrawable(R.drawable.ic_info_tab)), 
        KendalInfoFragment.class, null); 

      mTabHost.getTabWidget().setBackgroundColor(Color.WHITE); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

EventFragment.java:

public class EventFragment extends FragmentActivity { 

    private int userIcon, foodIcon, drinkIcon, shopIcon, otherIcon; 
    private GoogleMap theMap; 
    private LocationManager locMan; 
    private Marker userMarker; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

     View V = inflater.inflate(R.layout.event_layout, container, false); 

     // now run code below the inflate 

     if(theMap==null){ 
      //map not instantiated yet 
      theMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.the_map)).getMap(); 

     } 

     //theMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 

     //updatePlaces(); 

     theMap.setMyLocationEnabled(true); 

     theMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(54.328337, -2.743149) ,14)); 

     JSONArray arrayResultFromURL; 

     // store the result of the data into the variable 
     arrayResultFromURL = getJSONfromURL("http://www.websitewebsitestr.org/json.php?f=all&type=dateAndTime"); 
     try{ 
      //Log.d("array_len", String.format("%d", arrayResultFromURL.length())); 

      for(int i=0;i<arrayResultFromURL.length();i++){ 
       JSONObject json_data = arrayResultFromURL.getJSONObject(i); 

       // assign attributes from the JSON string to local variables 
       String id =json_data.getString("id"); 
       String title =json_data.getString("title"); 
       String strap; // define this for the try catch 

       // this try catch is in case the client does not 
       // put a strap in, title is mandatory 
       try{ 
        strap =json_data.getString("strap"); 
       }catch(Exception e){ 
        strap = ""; 
       } 

       Double lat = (double) 0; 
       Double lon = (double) 0; 

       // if the lat and long have not been entered then set them to 0 
       // otherwise an exception is thrown, and if it is lat and long will 
       // become 0, putting them down south. 
       try{ 
        lat = (double) json_data.getDouble("lat"); 
       }catch(Exception e){ 
        lat = (double) 0; 
       } 

       try{ 
        lon = (double) json_data.getDouble("long"); 
       }catch(Exception e){ 
        lon = (double) 0; 
       } 

       theMap.addMarker(new MarkerOptions() 
       .position(new LatLng(lat, lon)) 
       .title(title) 
       .snippet(strap)); 
      } 
     }catch (Exception e){ 
      Log.e("log_tag", "error in array: " + e.toString()); 
     } 


     //Log.d("log_tag", "result: " + res); 
     //Log.d("log_tag", "point debug!"); 

     //Log.d("myApp", res.toString()); 

     userIcon = R.drawable.common_signin_btn_icon_dark; 
     /*foodIcon = R.drawable.red_point; 
     drinkIcon = R.drawable.blue_point; 
     shopIcon = R.drawable.green_point; 
     otherIcon = R.drawable.purple_point;*/ 

     //return V; 
     return inflater.inflate(R.layout.event_layout, container, false); 
    } 

    public static JSONArray getJSONfromURL(String url){ 

     //initialize 
     InputStream is = null; 
     String result = ""; 
     JSONArray jArray = null; 

     //http post 
     try { 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url);  
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 

      is = entity.getContent();  

     } catch (Exception e) { 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 
     //convert response to string 

     try { 

      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line); 
      } 

      is.close(); 
      result=sb.toString(); 

     } catch (Exception e) {  
      Log.e("log_tag", "Error converting result "+e.toString()); 
     } 
     //try parse the string to a JSON object 

     try { 
      //Log.d("log_tag", "jresult: " + result + "finish"); 
      jArray = new JSONArray(result); 

      //Log.e("log_tag", "result: " + jArray.toString()); 

     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 

     return jArray; 
    } 


    public boolean onMarkerClick(Marker marker) { 
     Log.d("Debug_log", "clicked!!"); 
     return true; 
    } 

} 

它產生這樣的錯誤:

07-01 11:00:29.428: E/AndroidRuntime(24381): FATAL EXCEPTION: main 
07-01 11:00:29.428: E/AndroidRuntime(24381): java.lang.ClassCastException: co.uk.thewebsite.websitewebsitefest.EventFragment 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.support.v4.app.Fragment.instantiate(Fragment.java:394) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:276) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.View.dispatchAttachedToWindow(View.java:6203) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1174) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1179) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1179) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1179) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.ViewRoot.performTraversals(ViewRoot.java:797) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.view.ViewRoot.handleMessage(ViewRoot.java:1921) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.os.Looper.loop(Looper.java:143) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at android.app.ActivityThread.main(ActivityThread.java:4196) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at java.lang.reflect.Method.invokeNative(Native Method) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at java.lang.reflect.Method.invoke(Method.java:507) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-01 11:00:29.428: E/AndroidRuntime(24381): at dalvik.system.NativeStart.main(Native Method) 

我已經嘗試使用框架建議在其他stackoverflow帖子,但應用程序或者墜毀或bug out,我無法解決它。在過去的一週中,我在android中學到了很多東西,我很高興在將來學到更多東西!

回答

2

試試這個代碼

map = ((SupportMapFragment) getSupportFragmentManager() 
     .findFragmentById(R.id.map)).getMap(); 

,並導入

import android.support.v4.app.FragmentActivity; 

在你做這個 右擊項目 - >屬性 - > buildpath-> Java構建路徑 - >庫..然後點擊on add external jars

轉至

\android-sdks\extras\android\support\v4

,選擇Android的支持,v4.jar

+0

嘿感謝的評論,我已經添加了V4支持jar和遵循的步驟,我得到的'getSupportFragmentManager'一個錯誤,說這是不確定的類型EventFragment –

+0

擴展FragmentActivity ..擴展這個,看 –

+0

謝謝,這是排序錯誤。該應用程序仍然崩潰,但說'ClassCastException'爲錯誤 –