【问题标题】:ListView and custom SurfaceView in same fragment同一片段中的 ListView 和自定义 SurfaceView
【发布时间】:2017-01-19 03:49:45
【问题描述】:

我正在编写自定义 SurfaceView 实现:

CustomSurfaceView.java:

public class CustomSurfaceView extends SurfaceView {
  private void init() { }

  // ...
}

我想从片段myFragment 中调用CustomSurfaceView。我可以在网上找到的每个示例都是通过

class myFragment extends Fragment {
  // ...

  public View onCreateView() {
    return new CustomSurfaceView(getActivity());
  }
}

这种方法的问题是我在myFragment 中也有一个ListView,myList。它的工作原理是这样的:

my_fragment.xml:

<LinearLayout>
  <ListView id="@+id/my_list"/>
  <SurfaceView id="@+id/custom_surface_view"/>
</LinearLayout>

myFragment.java:

class myFragment extends Fragment {
  // ...

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);

    myList = (ListView) view.findViewById(R.id.my_list);

    return view;
  }
}

没有CustomSurfaceView 的配置可以正常工作。

然后,我尝试在myList = ...之后添加:

customSurfaceView = (CustomSurfaceView) view.findViewById(R.id.custom_surface_view);

导致Unexpected cast to CustomSurfaceView: layout tag was SurfaceView

如果在我的 xml 布局文件中将 &lt;SurfaceView/&gt; 更改为 &lt;com.example.CustomSurfaceView/&gt;,我会在运行时崩溃 (Binary XML : Error inflating class com.example.CustomSurfaceView)。

所以我的问题是:我做错了什么,将ListViewCustomSurfaceView 包含在同一个myFragment 中是正确的工作方式?

【问题讨论】:

    标签: java android listview android-fragments surfaceview


    【解决方案1】:

    问题是我没有被构造函数定义为正确的规范。

    将此代码添加到CustomSurfaceView.java 消除了error inflating 崩溃:

    public CustomSurfaceView(Context context, AttributeSet attrs) {
      super(context, attrs);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2014-04-28
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      相关资源
      最近更新 更多