0

怎么了伙计们,我有一个非常愚蠢的问题,但我真的无法解决这个问题。我正在尝试为片段设置扩展 SurfaceView 的视图。这是托管活动的 onCreate 方法:

public class ProbeMainActivity extends FragmentActivity{

BattleSurfaceView BSView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    BSView = new BattleSurfaceView(this);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    BattleFragment fragment = new BattleFragment();
    fragmentTransaction.add(BSView.getId(), fragment);
    fragmentTransaction.commit();

}

}

这是片段:

public class BattleFragment extends Fragment implements OnTouchListener
{   
/* Surface View for this activity */
BattleSurfaceView BSView;
DummySpell sp;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    /* instantiating the surface view */
    BSView = new BattleSurfaceView(this.getActivity());
    /* setting the listener - this, because it is defined within the activity */
    BSView.setOnTouchListener(this);

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return this.BSView;
}
    ...

}

每次,我都会收到“未找到视图”异常。我错过了什么?非常感谢,希望你能解决这个问题。干杯

4

1 回答 1

2

如果您只想在片段中显示您的自定义 SurfaceView ('MySurfaceView'),那么只需执行以下操作:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return new BattleSurfaceView(getActivity());
    }
于 2014-01-28T11:52:02.197 回答