2016-09-11 10 views
0

私はカスタムSurfaceViewの実装を書いている:私はフラグメントからCustomSurfaceViewを呼び出したいListViewコントロールとカスタムSurfaceView

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

    // ... 
} 

myFragment

CustomSurfaceView.java。私はウェブ上で見つけることができるすべての例では、このアプローチの問題は、私はまたmyFragmentで、リストビュー、myListを持っているということです

class myFragment extends Fragment { 
    // ... 

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

によってこれを行います。

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せず、正常に動作しますその構成、それはこのように動作します。 Unexpected cast to CustomSurfaceView: layout tag was SurfaceViewもたらす

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

を:

そして、IはmyList = ...後に追加しようとしました。

私のxmlレイアウトファイルで<SurfaceView/><com.example.CustomSurfaceView/>に変更すると、実行時にクラッシュする(Binary XML : Error inflating class com.example.CustomSurfaceView)。

ListViewCustomSurfaceViewを同じmyFragmentに含めることは間違っていますか?

答えて

0

問題は、コンストラクタによって適切な仕様に定義されていないことでした。 CustomSurfaceView.javaにこのコードを追加

error inflatingクラッシュを除去:

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