0
我想將我的Google地圖與Facebook SDK集成,通過Facebook檢查位置並以相同的佈局共享它,但是當我添加此代碼方法的onCreate(),它是強制關閉,並告訴一個錯誤RuntimeException:android.view.InflateException:二進制XML文件行#8:錯誤擴展類片段
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
myFragmentManager = getSupportFragmentManager();
mainFragment = (MainFragment) myFragmentManager.findFragmentById(R.id.checkIn);
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
}
,這裏是我的onCreate()方法
protected void onCreate(final Bundle savedInstanceState) {
try {
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.check_in);
checkInButton = (Button) findViewById(R.id.shareButton);
checkInButton.setVisibility(View.VISIBLE);
authButton = (Button)findViewById(R.id.authButton);
authButton.setVisibility(View.VISIBLE);
endLocationEditText = (EditText) findViewById(R.id.endLocationEditText);
endLocationEditText.setVisibility(View.INVISIBLE);
startLocationEdittext = (EditText) findViewById(R.id.starLocationEditText);
startLocationEdittext.setVisibility(View.INVISIBLE);
toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (hasConnection(getApplicationContext()) == true) {
if (toggle.isChecked()) {
endLocationEditText = (EditText) findViewById(R.id.endLocationEditText);
endLocationEditText.setVisibility(View.VISIBLE);
startLocationEdittext = (EditText) findViewById(R.id.starLocationEditText);
startLocationEdittext.setVisibility(View.INVISIBLE);
goButton.setVisibility(View.VISIBLE);
} else {
endLocationEditText = (EditText) findViewById(R.id.endLocationEditText);
endLocationEditText.setVisibility(View.INVISIBLE);
startLocationEdittext = (EditText) findViewById(R.id.starLocationEditText);
startLocationEdittext.setVisibility(View.VISIBLE);
goButton.setVisibility(View.VISIBLE);
}
checkInButton.setVisibility(View.VISIBLE);
checkInButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
endLocationEditText
.setVisibility(View.INVISIBLE);
AlertDialog.Builder builder = new AlertDialog.Builder(
CheckIn.this);
builder.setTitle("Attach photo?");
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
Intent captureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
captureIntent,
CAMERA_CAPTURE);
}
});
builder.show();
}
});
} else {
System.out.println("ยังไม่ได้ต่อเน็ต");
AlertDialog.Builder builder = new AlertDialog.Builder(
CheckIn.this);
builder.setTitle("Please connect to the Internet.");
builder.show();
}
}
});
goButton = (Button) findViewById(R.id.goButton);
goButton.setVisibility(View.INVISIBLE);
goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (toggle.isChecked() == true) {
String location = endLocationEditText.getText()
.toString();
if (location != null && !location.equals("")) {
new GeocoderTask().execute(location);
}
} else {
String location = startLocationEdittext.getText()
.toString();
if (location != null && !location.equals("")) {
new GeocoderTask().execute(location);
}
}
}
});
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = myLocationManager.getBestProvider(criteria, true);
Location location = myLocationManager
.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 20000, 0, this);
// สำหรับแสดง Google maps v2
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
.findFragmentById(R.id.checkIn);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
fromMarkerPosition = new LatLng(location.getLatitude(),
location.getLongitude());
toMarkerPosition = fromMarkerPosition;
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
fromMarkerPosition, 13));
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
myMap.addMarker(new MarkerOptions()
.position(fromMarkerPosition)
.title("Yor are here!")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
/* จบการแสดง maps */
// สร้าง click event สำหรับระบุพิกัดจุด
myMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng arg0) {
if (hasConnection(getApplicationContext()) == true) {
final LatLng coordinate = arg0;
AlertDialog.Builder builder = new AlertDialog.Builder(
CheckIn.this);
endLocationEditText.setVisibility(View.INVISIBLE);
startLocationEdittext.setVisibility(View.INVISIBLE);
goButton.setVisibility(View.INVISIBLE);
System.out
.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
builder.setTitle("Select Marker").setItems(
new String[] { "From", "To" },
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int position) {
try {
if (position == 0) {
fromMarkerPosition = coordinate;
System.out
.println(fromMarkerPosition
+ " yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
refreshMarker();
} else if (position == 1) {
toMarkerPosition = coordinate;
System.out
.println(toMarkerPosition
+ " ttttttttttttttttttttttttttttttttttttttt");
refreshMarker();
}
} catch (Exception ex) {
ex.printStackTrace();
System.out
.println("Please connect to the internet first");
}
}
});
builder.show();
myMap.animateCamera(CameraUpdateFactory
.newLatLng(coordinate));
checkInButton.setVisibility(View.VISIBLE);
checkInButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
endLocationEditText
.setVisibility(View.INVISIBLE);
AlertDialog.Builder builder = new AlertDialog.Builder(
CheckIn.this);
builder.setTitle("Attach photo?");
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
Intent captureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
captureIntent,
CAMERA_CAPTURE);
}
});
builder.show();
}
});
} else {
System.out.println("ยังไม่ได้ต่อเน็ต");
AlertDialog.Builder builder = new AlertDialog.Builder(
CheckIn.this);
builder.setTitle("Please connect to the Internet.");
builder.show();
}
}
});
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
myFragmentManager = getSupportFragmentManager();
mainFragment = (MainFragment) myFragmentManager
.findFragmentById(R.id.checkIn);
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}// end onCreate
這裏是我的MainFragment類........
public class MainFragment extends Fragment{
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private Button shareButton;
private UiLifecycleHelper uiHelper;
private static final String TAG = "MainFragment";
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.check_in, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
shareButton = (Button) view.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publishStory();
}
});
if (savedInstanceState != null) {
pendingPublishReauthorization =
savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
}
return view;
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
if (state.isOpened()) {
shareButton.setVisibility(View.VISIBLE);
if (pendingPublishReauthorization &&
state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
publishStory();
}
} else if (state.isClosed()) {
shareButton.setVisibility(View.INVISIBLE);
}
}
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "Facebook SDK for Android");
postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity()
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity()
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
uiHelper.onSaveInstanceState(outState);
}
}
XML 8號線在這裏
<fragment
android:id="@+id/checkIn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/authButton"
class="com.google.android.gms.maps.SupportMapFragment" />
這裏是一個錯誤
03-09 20:17:15.433: E/AndroidRuntime(12547): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
03-09 20:17:15.433: E/AndroidRuntime(12547): Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f04000a, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
在這裏你應該有一個堆棧跟蹤的「引起」部分。如果是這樣,請發佈。如果沒有,請確保您從'FragmentActivity'繼承,而不是'Activity'。 – CommonsWare 2013-03-09 13:02:52
是的,我沒有忘記從FragmentActivity繼承。 – 2013-03-09 13:26:30
您似乎試圖從'Fragment'的'onCreateView()'對包含'SupportMapFragment'的佈局進行膨脹。雖然嵌套的碎片確實是可能的,但我不確定這是否正確。 – CommonsWare 2013-03-09 13:37:27