我试图在sherlock framgment中添加自定义pan/touch/drag on SupportMapFragment,但我得到了这个logcat。我在这做错了什么?
04-23 13:36:19.725: E/AndroidRuntime(10848): FATAL EXCEPTION: main
04-23 13:36:19.725: E/AndroidRuntime(10848): java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to custom.widgets.CustomMapFragment
04-23 13:36:19.725: E/AndroidRuntime(10848): at fragment.lifestyle.LifeStyle.onActivityCreated(LifeStyle.java:171)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.os.Handler.handleCallback(Handler.java:730)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.os.Looper.loop(Looper.java:176)
04-23 13:36:19.725: E/AndroidRuntime(10848): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-23 13:36:19.725: E/AndroidRuntime(10848): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 13:36:19.725: E/AndroidRuntime(10848): at java.lang.reflect.Method.invoke(Method.java:525)
04-23 13:36:19.725: E/AndroidRuntime(10848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-23 13:36:19.725: E/AndroidRuntime(10848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-23 13:36:19.725: E/AndroidRuntime(10848): at dalvik.system.NativeStart.main(Native Method)
这是我的CustomMapFrament.java
package custom.widgets;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CustomMapFragment extends SupportMapFragment {
public View mOriginalContentView;
public TouchableWrapper mTouchView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
mOriginalContentView = super.onCreateView(inflater, parent,
savedInstanceState);
mTouchView = new TouchableWrapper(getActivity());
mTouchView.addView(mOriginalContentView);
return mTouchView;
}
@Override
public View getView() {
return mOriginalContentView;
}
}
这是我的TouchableWrapper.java文件
public class TouchableWrapper extends FrameLayout {
private long lastTouched = 0;
private static final long SCROLL_TIME = 100L; // 100 Milliseconds, but you
// can adjust that to your
// liking
private UpdateMapAfterUserInterection updateMapAfterUserInterection;
public TouchableWrapper(Context context) {
super(context);
// Force the host activity to implement the
// UpdateMapAfterUserInterection Interface
try {
updateMapAfterUserInterection = (UpdateMapAfterUserInterection) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement UpdateMapAfterUserInterection");
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastTouched = SystemClock.uptimeMillis();
break;
case MotionEvent.ACTION_UP:
final long now = SystemClock.uptimeMillis();
if (now - lastTouched > SCROLL_TIME) {
// Update the map
updateMapAfterUserInterection.onUpdateMapAfterUserInterection();
}
break;
}
return super.dispatchTouchEvent(ev);
}
// Map Activity must implement this interface
public interface UpdateMapAfterUserInterection {
public void onUpdateMapAfterUserInterection();
}
}
这是我的片段(Lifestyle.java)扩展了SherlockFragment。
public class LifeStyle extends SherlockFragment implements OnClickListener,
UpdateMapAfterUserInterection {
private View v;
private GoogleMap map;
private android.support.v4.app.FragmentManager fragmentManager;
private CustomMapFragment mapFragment;
public LifeStyle() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
}
try {
v = inflater.inflate(R.layout.lifstyle_fragment, container, false);
} catch (InflateException e) {
}
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fragmentManager = getChildFragmentManager();
mapFragment = (CustomMapFragment) fragmentManager
.findFragmentById(R.id.rel_map);
if (mapFragment == null) {
mapFragment = (CustomMapFragment) SupportMapFragment.newInstance();
fragmentManager.beginTransaction()
.add(R.id.rel_map, mapFragment).commit();
}
}
@Override
public void onUpdateMapAfterUserInterection() {
Toast.makeText(getActivity(), "Test", Toast.LENGTH_SHORT).show();
}
}
这是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/rel_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<My other views>
</RelaytiveLayout>
</RelaytiveLayout>
我已经尝试了这个代码,它的工作完美,但在片段中崩溃与上面给出的logcat。如何解决这个问题
答案 0 :(得分:1)
错误在于:
mapFragment = (CustomMapFragment) SupportMapFragment.newInstance();
SupportMapFragment.newInstance()
会创建一个SupportMapFragment
实例,而不是您的自定义类。你可能想写:
mapFragment = new CustomMapFragment();