Android fragments 从android中的片段调用surfaceview类中的函数

Android fragments 从android中的片段调用surfaceview类中的函数,android-fragments,methods,surfaceview,Android Fragments,Methods,Surfaceview,我有一个surfaceview类,其中有一个方法updateValue()。此曲面视图位于片段(framelayout)内部。我试图从fragment类调用updateValue()方法。但它不起作用 public class ImageSurface extends SurfaceView implements SurfaceHolder.Callback { ...... public void updateValue(int message){ .....

我有一个surfaceview类,其中有一个方法updateValue()。此曲面视图位于片段(framelayout)内部。我试图从fragment类调用updateValue()方法。但它不起作用

public class ImageSurface extends SurfaceView implements SurfaceHolder.Callback  {
......
        public void updateValue(int message){
        .......

        }
}
声明此surfaceview的xml文件。 fragment_main.xml--->

在上面的类中,有一个方法setMessage,我从中调用ImageSurface类的方法updateValue,但它给出了null点异常。我做错了什么


提前感谢。

我从onAttach()方法中删除了这一行

并在onCreateView()中插入了这一行

一切都很好

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.paint.drawx.MainActivity$PlaceholderFragment" >

<com.paint.drawx.ImageSurface
    android:id ="@+id/imagesurface1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
 <!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     >
</FrameLayout> 

 <fragment android:name="com.paint.drawx.DrawTools"
      android:id="@+id/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#FF889911"
                />
     </android.support.v4.widget.DrawerLayout>
 public static class PlaceholderFragment extends Fragment {
    ImageSurface imageSurface;
    public PlaceholderFragment() {
        //imageSurface = (ImageSurface)findViewById(R.id.imagesurface1);
    }
    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);

        imageSurface = (ImageSurface)activity.findViewById(R.id.imagesurface1);
    }

    public void setMessage(int mess){
        try{
        imageSurface.updateValue(mess);////////this is not working. gives error nullpoint exception
        }catch(Exception e){
            Log.d("error","is"+e.toString());
        }

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        setRetainInstance(true);
        return rootView;
    }
}
imageSurface = (ImageSurface)activity.findViewById(R.id.imagesurface1);
imageSurface = (ImageSurface) rootView.findViewById(R.id.imagesurface1);