Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-片段中的自定义视图在onResume()中不起作用_Android_Android View_Android Lifecycle - Fatal编程技术网

Android-片段中的自定义视图在onResume()中不起作用

Android-片段中的自定义视图在onResume()中不起作用,android,android-view,android-lifecycle,Android,Android View,Android Lifecycle,我已经创建了一个自定义视图来在屏幕上画一条线。此视图包含在片段的xml布局中,并在片段的onCreateView方法中检索,如下所示: MyCustomView mMyCustomView; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate view View v = inflater.inflate(R.l

我已经创建了一个自定义视图来在屏幕上画一条线。此视图包含在片段的xml布局中,并在片段的
onCreateView
方法中检索,如下所示:

MyCustomView mMyCustomView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate view
    View v = inflater.inflate(R.layout.fragment, container, false);

    mMyCustomView = (MyCustomView) v.findViewById(R.id.my_custom_view);
    ...
}
当我将
mMyCustomView
变量传递给片段的
onCreateView
方法中的自定义侦听器,并在侦听器类中调用类似
mMyCustomView.drawLine
的东西时,一切正常

然而,当我在片段的
onResume()
方法中调用
mMyCustomView.drawLine
时,什么也没有发生,尽管它是相同的变量和方法

我能想到的唯一原因是,侦听器在用户与片段交互时调用该方法,就生命周期而言,这甚至比调用
onResume()
还要晚。但是,在片段中,我不能在
onResume()中调用该方法

编辑1

这就是我的自定义视图的外观:

public class ConnectionLinesView extends View {
// Class variables
Paint mPaint = new Paint(); // Paint to apply to lines
ArrayList<float[]> mLines = new ArrayList<float[]>(); // Array to store the lines


public ConnectionLinesView(Context context) {
    super(context);

    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(2);
}


public ConnectionLinesView(Context context, AttributeSet attrs) { 
    super(context, attrs);

    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(2);
}


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

    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(2);
}


protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // If lines were already added, draw them
    if(!mLines.isEmpty()){
        for(float[] line : mLines){
            canvas.drawLine(line[0], line[1], line[2], line[3], mPaint);
        }
    }

}


// Method to add a line to the view
public void addLine(View v1, View v2) {
    float[] line = new float[4];
    line[0] = v1.getX() + v1.getWidth()/2;
    line[1] = v1.getY() + v1.getHeight()/2;
    line[2] = v2.getX() + v2.getWidth()/2;
    line[3] = v2.getY() + v2.getHeight()/2;

    mLines.add(line);
    this.invalidate();
}


public void removeLines() {
    mLines.clear();
    this.invalidate();
}

}
公共类连接视图扩展视图{
//类变量
Paint mPaint=new Paint();//要应用于线的绘制
ArrayList mLines=new ArrayList();//存储行的数组
公共连接线视图(上下文){
超级(上下文);
mPaint.setColor(Color.BLACK);
mPaint.设定行程宽度(2);
}
公共连接线视图(上下文,属性集属性){
超级(上下文,attrs);
mPaint.setColor(Color.BLACK);
mPaint.设定行程宽度(2);
}
公共连接线视图(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
mPaint.setColor(Color.BLACK);
mPaint.设定行程宽度(2);
}
受保护的void onDraw(画布){
super.onDraw(帆布);
//如果已经添加了线,请绘制它们
如果(!mLines.isEmpty()){
用于(浮动[]行:多行){
画布。绘制线(线[0],线[1],线[2],线[3],mPaint);
}
}
}
//方法将线添加到视图中
公共无效添加线(视图v1、视图v2){
浮动[]行=新浮动[4];
行[0]=v1.getX()+v1.getWidth()/2;
第[1]行=v1.getY()+v1.getHeight()/2;
行[2]=v2.getX()+v2.getWidth()/2;
第[3]行=v2.getY()+v2.getHeight()/2;
添加(行);
这个。使无效();
}
公共空间移除(){
mLines.clear();
这个。使无效();
}
}
当我在
onResume()
中调用addLine(…)时,即使到达了
onDraw()
方法中for循环的内部,也不会绘制这条线。当我稍后在listener类中添加另一行(它响应某些用户交互)时,这两行都在画布上绘制。不知怎的,
canvas.drawLine()
在视图父片段的
onResume()
中不起作用

编辑2


我添加了一个处理程序,在将片段添加到父活动的布局后,该处理程序会重复调用自定义视图的
invalidate
方法。这条线还没有画出来

最后,我通过创建一个处理程序解决了这个问题,该处理程序在添加片段50毫秒后调用
addLine
方法。一个相当脏的解决方案,但它能工作…

我还尝试在
onFocusChanged
onFocusChanged
方法中调用drawLine方法,但这也不起作用。没有人知道吗?你提到的是哪个听众?你能粘贴代码吗?还有,抽绳到底在做什么?