在Android中使用线程时获取Errs

时间:2012-04-20 16:56:12

标签: java android multithreading

I create a simple Project to load all images in drawable

这是我的代码

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list = (ImageView)findViewById(R.id.tung);
    ct = getApplicationContext();
    try
    {
        IDs = getAllResourceIDs(R.drawable.class);
        n= IDs.length;
        dem=0;
        list.setOnClickListener(new View.OnClickListener()
         {
            public void onClick(View v)
            {
                ++dem;
                list.setImageResource(IDs[dem]);
                if(dem==n-1)
                    dem=0;
            }
        });

        Thread x = new Thread(
                new Runnable ()
                {
                    public void run()
                    {
                        while(true)
                        {
                            try
                            {
                                ++dem;
                                //Toast.makeText(ct, ""+dem, 20).show();
                                list.setImageResource(IDs[dem]);
                                if(dem==n-1)
                                    dem=0;
                            }
                            catch(Exception e)
                            {
                                //Toast.makeText(ct,e.toString(), 200).show();
                            }
                        }
                    }
                }
                );
        x.start();
    }
    catch(Exception e)
    {
        Toast.makeText(this,e.toString(), 200).show();
    }
}

这里是getAllResourceIDs函数来获取所有ID

private int[] getAllResourceIDs(Class<?> aClass) throws IllegalArgumentException{
    /* Get all Fields from the class passed. */
    Field[] IDFields = aClass.getFields();

    /* int-Array capable of storing all ids. */
    int[] IDs = new int[IDFields.length];

    try {
            /* Loop through all Fields and store id to array. */
            for(int i = 0; i < IDFields.length; i++){
                    /* All fields within the subclasses of R
                     * are Integers, so we need no type-check here. */

                    // pass 'null' because class is static
                    IDs[i] = IDFields[i].getInt(null);
            }
    } catch (Exception e) {
            /* Exception will only occur on bad class submitted. */
            throw new IllegalArgumentException();
    }
    return IDs;

文件main.xml只有一个id =“tung”的ImageView 我试图使用名为x的Thead加载所有图像 通过使用代码 list.setImageResource(编号[DEM]); 正如你在我的代码中看到的那样 但事情没有发生 你能帮我解释一下吗?非常感谢!

2 个答案:

答案 0 :(得分:1)

初学者catch (Exception e)几乎绝不是一个好主意,因为它隐藏了发生错误时可以获得的所有有意义的信息。

您的错误特别在后台线程的这一行:

list.setImageResource(IDs[dem]);

不允许后台线程操纵UI对象。相反,您需要使用AsyncTask(更简单)或Handler(高级)

答案 1 :(得分:0)

JRaymond!非常感谢我创建一个Xthead类,像这样扩展Thread

class XThread extends Thread
{
    public int ids[];
    public ImageView icon;
    public int dem,n;
    XThread(int[] ids ,ImageView icon)
    {
        this.ids = ids;
        this.icon= icon;
        dem=0;
        n = ids.length;
    }

    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(500);
            handle.post(new Runnable()
            {
                public void run()
                {
                    ++dem;
                    icon.setImageResource(ids[dem]);
                    if(dem==n-1)
                        dem=0;
                }
            });
            }
            catch(Exception e)
            {

            }
        }
    }
}

和onCreate()函数

XThread x = new XThread(IDs,list);
        x.start();