leowong87
2022-01-18 17:42
采纳率: 75%
浏览 49

android studio开发 主页面获取fragment属性有误

问题遇到的现象和发生背景

我在程序的MainActivity中通过tablayout+viewPager的方式引入fragment 在fragment中摆放surfaceview控件并在surfaceChanged中对boolean变量赋值,在程序从后台重新返回前台时获取该boolean变量进行相关操作。发现在偶发的情况下该变量已经被设置为ture的情况下,主页面获取仍然一直为false

问题相关代码,请勿粘贴截图
package com.example.myapplication.tabviews;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.myapplication.BuildConfig;
import com.example.myapplication.MainActivity;
import com.example.myapplication.R;
import com.example.myapplication.module.SDKGuider;
public class TabViewPtz extends Fragment implements SurfaceHolder.Callback{
    private SurfaceView mRealView;
    private int m_iPreviewHandle;
    private boolean nmd = false;
    private boolean imok=false;  //供主页面访问的变量
    public int getM_iPreviewHandle() {
        return m_iPreviewHandle;
    }

    public void setM_iPreviewHandle(int m_iPreviewHandle) {
        this.m_iPreviewHandle = m_iPreviewHandle;
    }

    public SurfaceView getmRealView() {
        return mRealView;
    }

    public void setmRealView(SurfaceView mRealView) {
        this.mRealView = mRealView;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.viewpager_ptz,container,false);

        mRealView = (SurfaceView)view.findViewById(R.id.sV_PTZ1);
        mRealView.getHolder().addCallback(this);
        return view;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (BuildConfig.DEBUG) Log.d("TabViewPtz", "ptz界面销毁");
        nmd=false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (BuildConfig.DEBUG) Log.d("TabViewPtz", "Created");
        // 初始化海康
        ((MainActivity)getActivity()).initHCVideo();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (BuildConfig.DEBUG) Log.d("TabViewPtz", "想创建"+nmd);
        mRealView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        if(!nmd) return;
        Surface surface = holder.getSurface();
        if (surface.isValid()) {
            if (-1 == SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlaySurfaceChanged_jni(0, holder))
            {
                if (BuildConfig.DEBUG) Log.d("TabViewPtz", "创建报错"+ SDKGuider.g_sdkGuider.GetLastError_jni());
            }
            else
            {
                if (BuildConfig.DEBUG) Log.d("TabViewPtz", "创建成功");
            }
        }
//        nmd=false;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (BuildConfig.DEBUG) Log.d("TabViewPtz", "ptz界面变化:"+format+";"+width+";"+height);
        imok=true;  //在此对变量赋值
        nmd =true;
   }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (BuildConfig.DEBUG) Log.d("TabViewPtz", "PTZ销毁"+nmd);
        imok=false;
        if(!nmd) return;
        if (holder.getSurface().isValid()) {
            if (-1 == SDKGuider.g_sdkGuider.m_comPreviewGuider.RealPlaySurfaceChanged_jni(0, null))
            {
                if (BuildConfig.DEBUG) Log.d("TabViewPtz", "PTZ销毁报错"+ SDKGuider.g_sdkGuider.GetLastError_jni());
       //         Toast.makeText(getActivity().getApplicationContext(),"销毁报错"+ SDKGuider.g_sdkGuider.GetLastError_jni(),Toast.LENGTH_SHORT).show();
            }
        }
       nmd=false;

    }

//供主界面获取函数该变量的函数
    public boolean getSurfaceStatus()
    {
        return this.imok;
    }
}

Mainactivity代码如下
    TabLayout tabLayout;
    ViewPager viewPager;
    TabViewZK tabViewZK; // 类成员变量 liuzl
    TabViewPtz tabViewPtz; // 类成员变量 liuzl
    List<Fragment> fragments; //定义一个列表集合(应用泛型)
    String[] table={"PTZ相机","针孔相机"};  //定义一个数组存放标题内容

  @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ContextHolder.initial(this);
        setContentView(R.layout.activity_main);
        tabLayout=findViewById(R.id.tabs);
        viewPager=findViewById(R.id.vp_content);
        tabViewZK = new TabViewZK();
        tabViewPtz = new TabViewPtz();
        fragments=new ArrayList<>();  //实例化集合
        fragments.add(tabViewPtz);
        fragments.add(tabViewZK);
        Adapter adapter = new Adapter(getSupportFragmentManager(), fragments); //参数1为fragment管理器
        viewPager.setAdapter(adapter); //给viewPager设置适配器
        viewPager.addOnPageChangeListener(new myViewPagerChangeListener());
        tabLayout.setupWithViewPager(viewPager); //将tabLayout与viewPager建立匹配
    }
主界面按钮响应
 public void myButtonClick(@NonNull View v)
    {

        int i_buttonId=v.getId();
        switch (i_buttonId)
        {
            case R.id.button_play:
            {
                if (BuildConfig.DEBUG) Log.d(TAG, "tabViewPtz.getSurfaceStatus():" + tabViewPtz.getSurfaceStatus());
                break;
            }
}
运行结果及报错内容

当APP被从后台切换到前台时,绝大部分时候正常执行。偶发出现在Fragemnt中明明已经触发surfaceChanged对变量赋值为true切并未触发surfaceDestroyed的情况下,主页面通过getSurfaceStatus()方法获取到的值仍然为false。

我想要达到的结果

希望赐教,不胜感激

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题