【问题标题】:cannot make a static reference to the non-static method getactivity() from the type fragment无法从类型片段中对非静态方法 getactivity() 进行静态引用
【发布时间】:2015-01-05 09:24:53
【问题描述】:

我正在尝试在用户交互后重新调整我的地图。在我的项目中有一个这样的类:

public  class TouchableWrapper extends FrameLayout {

private long lastTouched = 0;
private static final long SCROLL_TIME = 200L;
private UpdateMapAfterUserInterection updateMapAfterUserInterection;

public TouchableWrapper(Context context) {


    super(context);
    // Force the host activity to implement the UpdateMapAfterUserInterection Interface
    try {

        updateMapAfterUserInterection = (StationsFragment.getActivity()) 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();
}

}

我的 StationsFragment 类包含并刷新地图。但在 TouchableWrapper 类中这一行

updateMapAfterUserInterection = (StationsFragment.getActivity()) context;

给出错误“无法从类型片段中对非静态方法 getactivity() 进行静态引用”。当我将 StationsFragment Fragment 的 Class 类型更改为 FragmentActivity 并像这样更改代码时:

  updateMapAfterUserInterection = (StationsFragment) context;

它有效。但我需要片段类。我该如何处理这个问题?

【问题讨论】:

    标签: java android android-fragments static android-context


    【解决方案1】:

    您可能在StationsFragment 类中实现UpdateMapAfterUserInterection 接口,然后将thisStationsFragment 传递给Class 对象到UpdateMapAfterUserInterection

     public TouchableWrapper(Context context,StationsFragment objStationsFragment) {
    
        super(context);
        updateMapAfterUserInterection = 
                   (UpdateMapAfterUserInterection) objStationsFragment;
    }   
    

    【讨论】:

      【解决方案2】:

      要实例化您的TouchableWrapper,无论如何您都需要一个context 对象,因为TouchableWrapper(Context context)。如果您提供给构造函数的上下文是承载您的Fragment 的活动之一,那么您可以安全地将上下文转换为Activity 的对象,如果此活动正在实现该StationsFragment 接口

      【讨论】:

        【解决方案3】:

        使用以下行..

        use updateMapAfterUserInterection = (UpdateMapAfterUserInterection) context;
        

        在您需要的片段中实现UpdateMapAfterUserInterection 接口。

        【讨论】:

        • 感谢您的回答。但 TouchableWrapper 类不是活动。所以 GetActivity 没用。
        • 抱歉,最初没有仔细检查您的代码,并从您最后几行问题中给出了答案..检查更新的答案
        猜你喜欢
        • 2013-01-15
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多