English 中文(简体)
2. 使用Thread In Anders
原标题:Get Errs when Using Thread In Android
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();
    }
}

页: 1 所有资源信息数据库都用于接收所有婴儿。

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;

the file main.xml has only a ImageView with id = "tung" I tried to load all images using a Thead named x by using code list.setImageResource(IDs[dem]); as you can see in my code but notthing is happen Can you explain it for me ! Thank a lot !

问题回答

对于开户人来说,<代码>副渔获物(Exception e)几乎永远不是一个好的想法,因为它掩盖了你在发生错误时能够取得的所有有意义的信息。

你的错误尤其出现在你的背景中:

list.setImageResource(IDs[dem]);

不允许背景线索操纵国际动因。 相反,需要使用AsyncTask(simpler)或Handler (Advanced)。

联合国 感谢大家,我设立了Xthead班级。 类似情况

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)
            {

            }
        }
    }
}

和(或)职能

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签