无法解析方法.runOnUiThread

the*_*ole 13 android listview

我发现这个位置上如何使用〔实施例runOnUiThreaed的方法,但我不知道如何使用它.

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

    // 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

    }
Run Code Online (Sandbox Code Playgroud)

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

public class ListAdapter {

    // some code

    ServiceHandler sh = new ServiceHandler();

    sh.run();
}
Run Code Online (Sandbox Code Playgroud)

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

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

    // some code

    list[0] = "foo";
    listAdapter.notifyDataSetChanged;
}
Run Code Online (Sandbox Code Playgroud)

但是我在运行时遇到这个错误

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

所以我试着解决错误 .runOnUiThread

所以这里再次是课堂上的.run()方法ServiceHandlerrunOnUiThread

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

    // some code

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

            list[0] = "foo";
            listAdapter.notifyDataSetChanged;
    });
}
Run Code Online (Sandbox Code Playgroud)

但我明白了

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

Vai*_*dey 13

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

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

}
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中实现它,如下所示:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 但我不想将服务处理程序移动到主要活动中。 (2认同)

Vic*_*ova 11

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