【问题标题】:Call a function in surfaceview class from a fragment in android从android中的片段调用surfaceview类中的函数
【发布时间】:2014-08-07 19:53:44
【问题描述】:

我有一个surfaceview 类,其中有一个方法updateValue()。此表面视图位于片段(框架布局)内。我正在尝试从片段类调用方法 updateValue() 。但它不起作用。

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

        }
}

xml 文件,其中声明了这个表面视图。 fragment_main.xml --->

<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>

activity_main.xml

<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;
    }
}

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

提前致谢。

【问题讨论】:

    标签: android-fragments methods surfaceview


    【解决方案1】:

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

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

    并在 onCreateView() 中插入这一行

    imageSurface = (ImageSurface) rootView.findViewById(R.id.imagesurface1);
    

    一切都很好

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 2014-09-17
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2013-10-28
      相关资源
      最近更新 更多