Android 尝试从另一个线程访问片段以更改TextView文本

Android 尝试从另一个线程访问片段以更改TextView文本,android,multithreading,textview,fragment,handler,Android,Multithreading,Textview,Fragment,Handler,我正在制作一个使用片段的应用程序,希望根据与笔记本电脑通信的客户端线程中接收到的消息更改TextView中的文本。客户机-服务器通信没有问题,因为客户机线程很好地接收到字符串 我似乎无法正确理解如何访问fragments TextView并更改其文本。 以下是我目前尝试的方法: class ClientThread implements Runnable { public void run() { mHandler.post(new Runnable() {

我正在制作一个使用片段的应用程序,希望根据与笔记本电脑通信的客户端线程中接收到的消息更改TextView中的文本。客户机-服务器通信没有问题,因为客户机线程很好地接收到字符串

我似乎无法正确理解如何访问fragments TextView并更改其文本。 以下是我目前尝试的方法:

class ClientThread implements Runnable {
    public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    LivingRoomFragment frag = (LivingRoomFragment)getSupportFragmentManager().findFragmentById(R.id.LivingRoomFragment);
                    frag.setText("Inside ClientThread right now");
                }
            });
    }
}


public static class LivingRoomFragment extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    TextView temp;

    public LivingRoomFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_room_control_fragment1, container, false);
        temp = (TextView) rootView.findViewById(R.id.textView5);
        MainActivity main = new MainActivity();
        new Thread(main.new ClientThread(requests)).start();
        return rootView;
    }   

    public void setText(String s){
        temp.setText(s);
    }
}
在本例中,MainActivity是扩展FragmentActivity的活动

我正在使用一个模拟器,应用程序总是崩溃,说现在使用frag.settextinsideclientthread的行中有一个空指针异常,我相信这意味着LivingRoomFragment的实例是空的。到目前为止,我的理解是,这应该使用处理程序来执行,因为如果不使用这样的方法,就无法从线程访问UI

我做错了什么?

我不确定,试试看

MainActivity main = (MainActivity)getActivity;
而不是

MainActivity main  = new  MainActivity();
好的,敬酒。 现在这是你的解决方案

在片段中创建广播接收器。 创造你的行动,行动是你广播中的关键

使用下面的示例代码。您没有发布更多的代码,因此它可能不符合您的要求,好吗

class ClientThread implements Runnable {
    private Handler mHandler;

    public void run() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {

                Intent intent = new Intent("my_action");
                intent.putExtra("message", "TEXT_YOU_WANT_TO_SET");
                sendBroadcast(intent);
                // LocalBroadcastManager manager = LocalBroadcastManager
                // .getInstance(context);
                // LivingRoomFragment frag = (LivingRoomFragment)
                // getSupportFragmentManager()
                // .findFragmentById(R.id.LivingRoomFragment);
                // frag.setText("Inside ClientThread right now");
            }
        });
    }
}

public static class LivingRoomFragment extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    TextView temp;
    private MyBroadCastReceiver broadCastReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        broadCastReceiver = new MyBroadCastReceiver();
        getActivity().registerReceiver(broadCastReceiver,
                new IntentFilter("my_action"));
    }

    public LivingRoomFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.activity_room_control_fragment1, container, false);
        temp = (TextView) rootView.findViewById(R.id.textView5);
        MainActivity main = new MainActivity();
        new Thread(main.new ClientThread(requests)).start();
        return rootView;
    }

    public void setText(String s) {
        temp.setText(s);
    }

    private class MyBroadCastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent intent) {
            // chaneg the TextView text here
            if (intent.getAction() != null
                    && intent.getAction().equalsIgnoreCase("my_action")) {
                temp.setText(intent.getStringExtra("message"));
            }
        }

    }

}

祝您好运。

您正在创建新的片段实例,而不是与当前片段通信。好的,您如何与当前片段通信?我以为getFragmentById得到的是当前片段,但我猜它实际做的是使用该片段ID创建一个新实例?你是对的。使用ID创建了一个空的新实例,通过在最初将LivingRoomFragment提交到片段管理器时设置一个标记,我可以稍后使用访问该片段。GetFragmentByTagTag尝试了这一点,但frag.setText仍给出了nullPointerException