1

我创建了一个滑块,在其中我从 xml 解析数据图像 url,它也在服务器上。我的应用程序在模拟器上运行良好,我还在清单文件中放置了互联网权限。但这在 android 设备中不起作用并且无法获取数据。请帮我!

public class SliderActivity extends Activity{
    int j=0;
    String imageList[];
    ImageView imageview;
    Button next_button, prev_button;
    int no_of_slides;

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageview = (ImageView) findViewById(R.id.imageView1);
        next_button= (Button) findViewById(R.id.next_button);
        prev_button= (Button) findViewById(R.id.prev_button);

            try{
                /** This section is for xml parsing */
                URL url = new URL("my url");
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new InputSource(url.openStream()));
                doc.getDocumentElement().normalize();
                NodeList nodeList = doc.getElementsByTagName("image");
                imageList= new String[nodeList.getLength()];
                no_of_slides=nodeList.getLength()-1;
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);

                    Element fstElmnt = (Element) node;
                    NodeList nameList = fstElmnt.getElementsByTagName("image");
                    Element nameElement = (Element) nameList.item(0);
                    nameList = nameElement.getChildNodes();

                    imageList[i]= ((Node) nameList.item(0)).getNodeValue();

                }
                next_button.setOnClickListener(new clicker());     
                prev_button.setOnClickListener(new clicker());
            }catch(Exception e){
                System.out.println("Exception appears = " + e);
            }
    }

    class clicker implements Button.OnClickListener{              
        public void onClick(View v){
            /** This section is for next button */
            if(v==next_button){
                if(j==no_of_slides)
                    j=0;
                else
                    j=j+1;
                try{
                    imageview.setImageDrawable(grabImageFromUrl(imageList[j]));
                }catch(Exception e){
                    System.out.println("Exception appears = " + e);  
                }
            }
            else
                /** This section is for prev button */
                if(v==prev_button){
                    if(j==0)
                        j=no_of_slides;
                    else
                        j=j-1;
                    try{
                        imageview.setImageDrawable(grabImageFromUrl(imageList[j]));
                    }catch(Exception e){
                        System.out.println("Exception appears = " + e);  
                    }       
                }
            }
    }

    /** This function is to fetch image from URL */
    private Drawable grabImageFromUrl(String url) throws Exception {
        return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
    }
}

XML 结构

<images>
    <image>http://www.xyz.com/1.jpeg</image>
    <image>http://www.xyz.com/1.jpeg</image>
    <image>http://www.xyz.com/1.jpeg</image>
</images>

错误的主要原因

11-09 10:08:56.103: E/System(61): Failure starting core service

11-09 10:08:56.103: E/System(61): java.lang.SecurityException

11-09 10:08:56.103: E/System(61):   at android.os.BinderProxy.transact(Native Method)

11-09 10:08:56.103: E/System(61):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)

11-09 10:08:56.103: E/System(61):   at android.os.ServiceManager.addService(ServiceManager.java:72)

11-09 10:08:56.103: E/System(61):   at com.android.server.ServerThread.run(SystemServer.java:176)

我在 logcat 中收到这些消息.....

4

2 回答 2

0

我认为您必须将图像缓存到本地,请参阅Android:图像下载和缓存

于 2012-11-09T09:55:39.737 回答
0

数据是否在手机上激活?

于 2012-11-09T10:39:28.887 回答