我一直在为我在xml上定义的linearlayout对象获取一个空指针异常。该对象已添加到我在setcontentview()方法中设置的布局中。线性布局显示在屏幕上。我还将一个对象动画师应用于linearlayout对象,它在屏幕上移动。 当我尝试使用此xml linearlayout的getX()和getY()值计算for循环中的计算时,我得到空指针异常。这个for循环驻留在一个匿名线程中,该线程在方法内初始化。嗯,这是代码。如果您看到错误,请告诉我:
LinearLayout xml_linearlayout
//inside onCreate
xml_linearlayout = (LinearLayout)getLayoutInflater.inflate(R.layout.mm,null);//i think here's problem
//here is my main layout called main. Its a relative layout
//this main layout was created programmatically not by xml format
main.addView(xml_linearlayout,parameters that have MATCH_PARENT= width and height);
//onCreate has ended
//method that attempts to use getX() and getY() of xml_linearlayout
public void methodCal(final float x, final float y){
float newX = x;
float newY = y;
new Thread(new Runnable(){
public void run(){
for(int i=0; i<30 ; i++){
double e = Math.pow(newX-xml_linearlayout.getX(),2.0);// NPE thrown here
}
}
}).start();
}