Android - View资源在PageViewer中的片段上的事件内为null

时间:2014-06-09 20:35:40

标签: android null resources fragment

我在使用PageViewer时遇到了一些麻烦,我在其他问题上搜索了很多,但似乎没有人遇到同样的麻烦,所以我在这里向专家询问我在做什么错。

问题在于:

我无法弄清楚事件中的布局是否膨胀,这是代码:

    // onCreateView :
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        Bundle savedInstanceState) {

    // fragment not when container null
    if (container == null) {
        return null;
    }
    // inflate view from layout
    View view = (RelativeLayout) inflater.inflate(R.layout.fragment_doorsway_lay, container, false);
    // get button reference
    /*
     * btnWrite = (Button) view.findViewById(R.id.btWrite); // set button
     * listener to trigger activity listener btnWrite.setOnClickListener(new
     * View.OnClickListener() {
     * 
     * @Override public void onClick(View v) {
     * pageListener.onPage1("Page 1 to"); } }); // update text TextView tv =
     * (TextView) view.findViewById(R.id.tvText1); tv.setText(ptext);
     */
    View imageView = view.findViewById(R.id.clickZones);
    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            View view = (RelativeLayout) inflater.inflate(R.layout.fragment_doorsway_lay, container, false);
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // PRESSED
                try {
                    ImageView newView = ((ImageView) v);
                    Bitmap bitmap = ((BitmapDrawable) newView.getDrawable()).getBitmap();
                    Matrix inverse = new Matrix();
                    newView.getImageMatrix().invert(inverse);
                    float[] touchPoint = new float[] { event.getX(),
                            event.getY() };
                    inverse.mapPoints(touchPoint);
                    int pixel = bitmap.getPixel((int) touchPoint[0],
                            (int) touchPoint[1]);
                    int redValue = Color.red(pixel);
                    int greenValue = Color.green(pixel);
                    if (greenValue == 255) {    

                        //pageListener.onPage1("GREEN");   
                        view.findViewById(R.id.imgChainPressSx).setVisibility(View.VISIBLE);
                        //view.findViewById(R.id.imgChainPressSx).setVisibility(View.VISIBLE);
                    }
                    if (redValue == 255) {
                        //pageListener.onPage1("RED");    
                        view.findViewById(R.id.imgChainPressDx).setVisibility(View.VISIBLE);
                    }
                    // new Connection().execute();
                } catch (Exception e) {
                    Log.e("error", "" + e.getMessage());
                }
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                  // RELEASED
                //view.findViewById(R.id.imgChainPressSx).setVisibility(View.VISIBLE);
                //view.findViewById(R.id.imgChainPressDx).setVisibility(View.VISIBLE);
            }
            return true;
        }
    });
    return view;
}

代码工作,btu可能是因为第二次调用:

View view = (RelativeLayout) inflater.inflate(R.layout.fragment_doorsway_lay, container, false);

未引用主视图,即行中可见的视图

view.findViewById(R.id.imgChainPressDx).setVisibility(View.VISIBLE);

不会显示在GUI中。 ....在现实中,我无法弄明白......

使用:

pageListener.onPage1("GREEN");

这是一个在main活动中工作的函数,传递一个字符串并处理它,我可以得到主视图只询问“findViewById”而不引用一个视图,并用字符串上的if语句管理它,但是这个不是我想要处理的方式......我知道这不是最好的方法......

提前感谢您回复并向大家致以最诚挚的问候。

Fin3

2 个答案:

答案 0 :(得分:0)

检查一下:

  • 您的片段是RegisterFragment的子类吗?
  • 标准错误:' fragment_doorsway_lay'写的没错?

(很抱歉,我没有评论,但我的声誉太低了......)

答案 1 :(得分:0)

感谢您的回复,无论如何我找到了解决方案......看起来像是:

    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        Bundle savedInstanceState) {

    // fragment not when container null
    if (container == null) {
        return null;
    }
    // inflate view from layout
    View view = (RelativeLayout) inflater.inflate(R.layout.fragment_doorsway_lay, container, false);
    // get button reference
    /*
     * btnWrite = (Button) view.findViewById(R.id.btWrite); // set button
     * listener to trigger activity listener btnWrite.setOnClickListener(new
     * View.OnClickListener() {
     * 
     * @Override public void onClick(View v) {
     * pageListener.onPage1("Page 1 to"); } }); // update text TextView tv =
     * (TextView) view.findViewById(R.id.tvText1); tv.setText(ptext);
     */
    //View imageView = view.findViewById(R.id.clickZones);
    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // PRESSED
                try {
                    ImageView newView = ((ImageView) v.findViewById(R.id.clickZones));
                    Bitmap bitmap = ((BitmapDrawable) newView.getDrawable()).getBitmap();
                    Matrix inverse = new Matrix();
                    newView.getImageMatrix().invert(inverse);
                    float[] touchPoint = new float[] { event.getX(),
                            event.getY() };
                    inverse.mapPoints(touchPoint);
                    int pixel = bitmap.getPixel((int) touchPoint[0],
                            (int) touchPoint[1]);
                    int redValue = Color.red(pixel);
                    int greenValue = Color.green(pixel);
                    if (greenValue == 255) {    

                        //pageListener.onPage1("GREEN");   //Questa funzione finisce nel Main_Activity
                        v.findViewById(R.id.imgChainPressSx).setVisibility(View.VISIBLE);
                        //view.findViewById(R.id.imgChainPressSx).setVisibility(View.VISIBLE);
                    }
                    if (redValue == 255) {
                        //pageListener.onPage1("RED");    //Questa funzione finisce nel Main_Activity
                        v.findViewById(R.id.imgChainPressDx).setVisibility(View.VISIBLE);
                    }
                    // new Connection().execute();
                } catch (Exception e) {
                    Log.e("error", "" + e.getMessage());
                }
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                  // RELEASED
                v.findViewById(R.id.imgChainPressSx).setVisibility(View.INVISIBLE);
                v.findViewById(R.id.imgChainPressDx).setVisibility(View.INVISIBLE);
            }
            return true;
        }
    });
    return view;
}

所以,我没有引用导致onTouch重写事件的特定视图,但是必须传递整个视图,pageViewer给出的视图,然后在方法中使用findViewById访问特定视图。

希望这会对某人有所帮助!

祝你好运,

Fin3