【问题标题】:Cannot resolve method .runOnUiThread无法解析方法 .runOnUiThread
【发布时间】:2020-06-09 01:54:05
【问题描述】:

我在这里找到了this 示例,了解如何使用runOnUiThread 方法,但我不明白如何使用它。

我在我的主要活动中设置了列表适配器

    // Set a gobal reference to the list adapter and the list respectivly 

    ListAdapter listAdapter;
    static ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // some code

        adapter = new ListAdapter(getActivity(), R.layout.item_layout, list);
        listView.setAdapter(adapter);

        // some code

    }

我这里的列表适配器调用了一个服务处理程序类

public class ListAdapter {

    // some code

    ServiceHandler sh = new ServiceHandler();

    sh.run();
}

这是ServiceHandler 类中的.run() 方法,我在其中更新了列表和列表适配器

public void run(Adapter listAdapter, ArrayList<String> list){

    // some code

    list[0] = "foo";
    listAdapter.notifyDataSetChanged;
}

但我在运行时收到此错误

只有创建视图层次结构的原始线程才能接触其视图。

所以我尝试用.runOnUiThread解决错误

所以这里又是ServiceHandler 类中的.run() 方法,runOnUiThread

public void run(Adapter listAdapter, ArrayList<String> list){

    // some code

    runOnUiThread(new Runnable() {
        @Override
        public void run() {

            list[0] = "foo";
            listAdapter.notifyDataSetChanged;
    });
}

但我明白了

无法解析方法'runOnUiThread(anonymous Java.lang.runnable)'

【问题讨论】:

    标签: android listview


    【解决方案1】:

    您可以将服务处理程序作为 private 成员类移动到您的 Activity 中,如下所示,以使用 Activity 的 this 上下文。

    class MyActivity extends Activity {
    
        private class ServiceHandler /** Whichever class you extend */ {
            public void run() {
                MyActivity.this.runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                    }
    
                });
            }
        }
    
        private MyListAdapterTracks mAdapter;
    
        private ServiceHandler mHandler;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Some code
    
            mAdapter = new MyListAdapterTracks(getActivity(), R.layout.item_layout, list);
            listView.setAdapter(mAdapter);
    
            // Some code
    
            ServiceHandler sh = new ServiceHandler();
            sh.run();
        }
    }
    

    编辑:

    public class ServiceHandler /** Whichever class you extend */ {
    
        private final Activity mActivity;
    
        private final MyListAdapterTracks mAdapter;
    
        public ServiceHandler(Activity activity, MyListAdapterTracks adapter) {
            mActivity = activity;
            mAdapter = adapter;
        }
    
        @Override
        public void run() {
            mActivity.runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    mAdapter.notifyDataSetChanged();
                }
    
            });
        }
    
        // More code
    
    }
    

    然后像这样在你的活动中实例化它:

    class MyActivity extends Activity {
    
        private MyListAdapterTracks mAdapter;
    
        private ServiceHandler mHandler;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Some code
    
            ServiceHandler sh = new ServiceHandler(this);
            sh.run();
        }
    }
    

    【讨论】:

    • 但我不想将服务处理程序移动到主要活动中。
    • 在这种情况下,您可以在 ServiceHandler 的构造函数中传递对您的活动的引用。请参阅上面的编辑。
    • private final Activity mActivity; 是什么意思?
    • private final 将私有成员标记为 final,这意味着它们不能在构造函数的其他地方重新分配。
    【解决方案2】:

    我有同样的问题,我用getActivity().runOnUiThread() 替换了runOnUiThread()。它奏效了。

    【讨论】:

    • 如果我使用适配器会怎样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2016-01-15
    • 2015-06-10
    • 2019-08-28
    • 2019-10-10
    相关资源
    最近更新 更多