我在一个片段中有一个片段活动有一些文本字段和一个mapview。第二个片段有一个显示新闻项目的文本字段。第一个片段在片段内有地图。所以这是嵌套片段的情况。 / p>
片段活动是:
package com.igloo.collegefinder;
import com.igloo.classes.College_data;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import com.google.android.gms.maps.SupportMapFragment;
public class FragmentMain extends FragmentActivity {
LinearLayout footer;
Button btndetails,btncollegenews;
College_data college_item;
Fragment fr;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
footer=(LinearLayout) findViewById(R.id.footer);
btndetails=(Button) footer.findViewById(R.id.btndetails);
btncollegenews=(Button) footer.findViewById(R.id.btncollegenews);
college_item= (College_data) getIntent().getExtras().getSerializable("college_data");
// Fragment fragment = new Fragment();
final Bundle bundle = new Bundle();
bundle.putSerializable("college_data",college_item);
final CollegeDetails fragobj1=new CollegeDetails();
fragobj1.setArguments(bundle);
final NewsDetails fragobj2=new NewsDetails();
fragobj2.setArguments(bundle);
fr=fragobj1;
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place,fr);
fragmentTransaction.commit();
btndetails.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
fr=new CollegeDetails();
fr.setArguments(bundle);
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place,fr);
fragmentTransaction.commit();
}
});
btncollegenews.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fr=new NewsDetails();
fr.setArguments(bundle);
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place,fr);
fragmentTransaction.commit();
}
});
}
}
第一个片段是:
package com.igloo.collegefinder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.igloo.classes.College_data;
import com.igloo.classes.News_data;
import com.igloo.collegefinder.CollegeMain.RetrieveColleges;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class CollegeDetails extends Fragment {
TextView txtcollegename,txtcollegeaddress,txtcollegepho,txtcollegelatitude,txtcollegelongitude,txtcollegeimage,txtcollegenews;
String college_name,course_name;
ProgressDialog progressDialog;
String URL="http://footballultimate.com/finder/index.php/api/collegeDataWithoutFilter";
HttpResponse response;
String result,news_value="";
JSONObject jsonobject;
JSONArray college_data;
College_data college_item;
Bundle b;
View v;
FragmentManager fragManager;
private FragmentActivity myContext;
// Google Map
private GoogleMap googleMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//setContentView(R.layout.college_details);
v=inflater.inflate(R.layout.college_details, null, false);
txtcollegename=(TextView)v.findViewById(R.id.txtcollegename);
txtcollegeaddress=(TextView)v.findViewById(R.id.txtcollegeaddress);
txtcollegepho=(TextView)v. findViewById(R.id.txtcollegepho);
txtcollegelatitude=(TextView)v. findViewById(R.id.txtcollegelatitude);
txtcollegelongitude=(TextView)v. findViewById(R.id.txtcollegelongitude);
txtcollegeimage=(TextView)v. findViewById(R.id.txtcollegeimage);
txtcollegenews=(TextView)v. findViewById(R.id.txtcollegenews);
//college_item= (College_data)getActivity().getIntent().getExtras().getSerializable("college_data");
college_item=(College_data) getArguments().getSerializable("college_data");
txtcollegename.setText(college_item.collegename);
txtcollegeaddress.setText(college_item.collegeaddress);
txtcollegepho.setText(college_item.collegephone);
txtcollegelatitude.setText(college_item.collegelatitude);
txtcollegelongitude.setText(college_item.collegelongitude);
txtcollegeimage.setText(college_item.collegeimage);
// for(News_data item : college_item.news_list){
// news_value=news_value+item.news+" ";
// }
// txtcollegenews.setText(news_value);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
private void initilizeMap() {
if (googleMap == null) {
fragManager = myContext.getSupportFragmentManager();
googleMap = ( (SupportMapFragment) fragManager.findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
else
{
//googleMap.setMyLocationEnabled(true);
// latitude and longitude
double latitude =Double.parseDouble(college_item.collegelatitude);
double longitude =Double.parseDouble(college_item.collegelongitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(college_item.collegename);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// adding marker
googleMap.addMarker(marker);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),12.0f));
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
第一个片段的xml文件是:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegename"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegeaddress"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegepho"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegelatitude"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegelongitude"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegeimage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtcollegenews"
/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
第二个片段是:
package com.igloo.collegefinder;
import com.igloo.classes.College_data;
import com.igloo.classes.News_data;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class NewsDetails extends Fragment {
View v;
TextView txtnewsfinal;
College_data college_item;
String news_value="";
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
v=inflater.inflate(R.layout.news_details, null, false);
college_item=(College_data) getArguments().getSerializable("college_data");
txtnewsfinal=(TextView) v.findViewById(R.id.txtnewsfinal);
for(News_data item : college_item.news_list){
news_value=news_value+item.news+" ";
}
txtnewsfinal.setText(news_value);
return v;
}
}
第二个xml文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtnewsfinal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
问题如下:
当片段主要加载时,世界地图显示在后台,然后所需的地图如下所示。这是因为fragment_main中的class =“com.google.android.gms.maps.SupportMapFragment” .XML
当我转到第二个片段时,会显示新闻项目。但是当我单击第一个片段的按钮时,应用程序力将关闭以下LOGCAT:
07-14 13:58:15.906: E/AndroidRuntime(10453): FATAL EXCEPTION: main
07-14 13:58:15.906: E/AndroidRuntime(10453): android.view.InflateException: Binary XML file line #43: Error inflating class fragment
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-14 13:58:15.906: E/AndroidRuntime(10453): at com.igloo.collegefinder.CollegeDetails.onCreateView(CollegeDetails.java:65)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.os.Handler.handleCallback(Handler.java:587)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.os.Looper.loop(Looper.java:130)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-14 13:58:15.906: E/AndroidRuntime(10453): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 13:58:15.906: E/AndroidRuntime(10453): at java.lang.reflect.Method.invoke(Method.java:507)
07-14 13:58:15.906: E/AndroidRuntime(10453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-14 13:58:15.906: E/AndroidRuntime(10453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-14 13:58:15.906: E/AndroidRuntime(10453): at dalvik.system.NativeStart.main(Native Method)
07-14 13:58:15.906: E/AndroidRuntime(10453): Caused by: java.lang.IllegalArgumentException: Binary XML file line #43: Duplicate id 0x7f06001e, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
07-14 13:58:15.906: E/AndroidRuntime(10453): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
07-14 13:58:15.906: E/AndroidRuntime(10453): ... 19 more
它表明class =“com.google.android.gms.maps.SupportMapFragment”元素是重复的。因为片段活动和第一个片段具有相同的名称。但没有它,片段活动就不起作用。请帮忙!!
答案 0 :(得分:0)
由于这种情况,最好使用 FragmentManager 和 FrameLayout 以编程方式添加所有片段,就像对第一个Activity一样。< / p>
唯一的区别是,要使用嵌套片段,您需要在父片段中调用 getChildFragmentManager getFragmentManager 。
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame_layout_container, mapFragment);
transaction.commit();
答案 1 :(得分:0)
您必须正确销毁GoogleMap,否则您将获得此例外。 在Map-Fragment中覆盖此方法,它应该起作用:
@Override
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}