从非片段类中的片段引用findViewbyID

斯特凡

我有一个Fragment类EchossStampFragment,可以直接在其中引用

.findViewbyId(R.id.mainView)

在我的第二个非片段类课程中,EchossMainView我想讲解此View并更改其背景。

我该如何在onInitSuccess中解决它,如下所示:

public void onInitSuccess() {
        // EchossStampActivity.setText("onInitSuccess");
        mainView.setBackgroundColor(Color.RED);
    }

EchossMainView上课吗?

EchossStampFragment类

public class EchossStampFragment extends Fragment implements EchossStampInterface {
    private static final String TAG = MainActivity.class.getSimpleName();
    View myView;
    private SupportMapFragment mapFragment;
    EchossMainView mainView = null;
    static TextView textView = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.activity_echoss, container, false);
        return myView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mainView = (EchossMainView) getView().findViewById(R.id.mainView);
        textView = (TextView) getView().findViewById(R.id.textView);
        FragmentManager fm = getChildFragmentManager();

        // Display Initialization

        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        mainView.init(metric);

        mainView.initEchossMainView(this); // this is where we are passing the current activity as interface

        // Library Initialization for Stamp
        mainView.echossPlatformInit(APIKEY, REGION, LANGUAGE, INDEX);

    }

EchossMainView类别

public class EchossMainView extends StampBaseView {

    private EchossStampInterface mEchossStampInterface;

    public EchossMainView(Context context) {
        super(context);
    }

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

    public EchossMainView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void onInitSuccess() {
        EchossStampActivity.setText("onInitSuccess");
        mainView.setBackgroundColor(Color.RED);

    }
    public void onInitError(String code, String message) {
        EchossStampFragment.setText("onInitError - " + code + " " + message);

    }


    public void onStampSuccess(String stampResult) {
        EchossStampFragment.setText("onStampSuccess - " + stampResult);

    }

    public void onStampError(String code, String message) {
         EchossStampFragment.setText("onStampError - " + code + " " + message);
    }
}
亚当·麦克331

由于EchossMainView必须最终从View扩展,因此可以从该类本身内部调用setBackground:

public void onInitSuccess() {
    setBackgroundColor(Color.RED);
}

此处的方法文档:https : //developer.android.com/reference/android/view/View.html#setBackgroundColor(int)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

片段类中的View.findViewById

来自分类Dev

findViewById在片段类中返回null

来自分类Dev

findViewById在片段类中返回null

来自分类Dev

引用子片段中的片段

来自分类Dev

无法解析非片段类中的 getSupportFragmentManager

来自分类Dev

片段onAttach中的NullPointerException for findViewById

来自分类Dev

在片段中引用SupportFragmentManager

来自分类Dev

在片段中引用SupportFragmentManager

来自分类Dev

片段类中的getApplicationContext()

来自分类Dev

片段类中的getApplicationContext()

来自分类Dev

FindViewbyId总是在片段中返回Null

来自分类Dev

无法解析片段中的方法“ findViewById(int)”

来自分类Dev

FindviewbyId方法在片段中不起作用

来自分类Dev

在片段中调用findViewById返回null

来自分类Dev

“无法解析片段中的 findviewbyid”错误

来自分类Dev

在片段内使用findViewById

来自分类Dev

FindViewByID片段为空

来自分类Dev

FindViewByID片段为空

来自分类Dev

findViewById用于非活动片段选项卡

来自分类Dev

非活动类中的findViewById

来自分类Dev

片段类中的致命错误

来自分类Dev

在片段类中操作变量

来自分类Dev

如何从类中调用片段

来自分类Dev

将片段列表作为扩展FragmentStatePagerAdapter的类中的类引用来保存是否可以

来自分类Dev

包含片段的帧布局中的视图的findViewById失败

来自分类Dev

我如何使用片段代替onCreateView中的findViewById

来自分类Dev

如何在片段类的viewpager中显示片段?

来自分类Dev

片段未定义findViewById

来自分类Dev

Android片段-findViewById返回null