java.lang.ClassCastException:java.lang.Object []无法强制转换为android.graphics.Point []

时间:2014-01-22 00:02:31

标签: java android json uiautomator

public class SendText extends UiAutomatorTestCase {

    private String jsonString = "{\"A\": [[107,729],[108,728],[108,728],[108,727]," +
        "[108,727],[108,727],[108,727],[108,727],[108,727],[108,727],[108,727],[108,727],[110,724]," +
        "[114,717],[125,701],[134,685],[145,663],[157,636],[169,607],[179,583],[191,558],[196,547]," +
        "[199,540],[201,534],[202,529],[203,528],[204,525],[204,524],[204,524],[204,524],[204,524]," +
        "[204,524],[204,525],[204,530],[205,538],[207,549],[209,566],[216,602],[223,629],[229,653]," +
        "[235,671],[238,684],[241,697],[242,703],[242,707],[243,710],[243,712],[243,713],[243,713]," +
        "[243,713],[243,713],[244,713],[244,713],[244,713],[244,713],[242,709],[237,703],[230,695]," +
        "[224,684],[220,679],[215,671],[212,665],[206,658],[202,655],[200,654],[196,651],[192,648]," +
        "[189,646],[184,642],[182,641],[180,639],[179,637],[177,635],[176,634],[174,633],[173,631]," +
        "[172,630],[171,629],[170,628],[170,628],[170,627],[169,627],[169,627],[169,627],[169,627]," +
        "[169,629],[169,629]]}" ;

    public void testSendText() throws UiObjectNotFoundException {   

        JSONObject jsonObj;
        try {
            jsonObj = new JSONObject(jsonString);
            jsonObj.getJSONArray("A");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //String line = null;

        for(int k = 0; k < jsonString.length(); k++ ){
            Vector<Point> apoints = new Vector<Point>();
            apoints.add(new Point());
            getUiDevice().swipe((Point[])apoints.toArray(), 5);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

集合界面有2个toArray方法。第一个,Object [] toArray()返回集合中的所有元素,并将它们存储在Object []类型的数组中。第二个,T [] toArray(T [] a)将参数中的集合元素存储到方法中,如果它足够大,否则它通过运行时反射在a上分配一个适当大小的数组。您的代码相当于以下内容: `

Object[] x = new Object[1];

x[0] = "Test";
System.out.println(((String[]) x)[0]); //Throws a ClassCastException

` 为了解决这个问题,只需按照ZouZou的建议使用toArray(T [] a)版本的toArray。