3

I call this method at start Activity. I want it to be displayed in RecycleView this my code and give me Exception

public class FirstFragment extends Fragment {

//private
String[] username = {"joe", "mido", "star", "fawzy", "mohsen"};
private RecyclerView recyclerView;
TextView textView;
ProgressDialog dialog;
FirstpageAdapter adapter;
View layout;
private final Handler myHandler = new Handler();
List<String> ids = new ArrayList<>();
private final static String TAG = "FIRST_FRAGMENT";
ListView listView;
Runnable updateRunnable;

public FirstFragment() {
    // Required empty public constructor
}

public List<String> getData() {
    List<String> data = new ArrayList<>();
    for (int i = 0; i < username.length; i++) {
        data.add(username[i]);
    }
    return data;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //new MyTask().execute();
    View layout = inflater.inflate(R.layout.fragment_first, container, false);
    recyclerView=(RecyclerView)layout.findViewById(R.id.firstList);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    PubNubService.herNow(new Callback() {
        public void successCallback(String channel, Object message) {
            try {
                JSONObject object = new JSONObject(message.toString());
                JSONArray jsonArray = object.getJSONArray("uuids");
                for (int i = 0; i < jsonArray.length(); i++) {
                    ids.add(jsonArray.getString(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.e(TAG, ids.size() + "");
            Log.e(TAG, message.toString());

                    adapter = new FirstpageAdapter(getActivity(), ids);
            recyclerView.setAdapter(adapter);

        }

        @Override
        public void errorCallback(String s, PubnubError pubnubError) {
            Log.e(TAG, "error Callback" + pubnubError.getErrorString());
        }
    });

    return layout;
}

}

Exception

08-01 14:16:06.903 12365-12490/com.sprintone E/AndroidRuntime﹕ FATAL EXCEPTION: Non-Subscribe-Manager-1384510668-5 Process: com.sprintone, PID: 12365 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024) at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:820) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.support.v4.widget.DrawerLayout.requestLayout(DrawerLayout.java:979) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.view.View.requestLayout(View.java:16431) at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:2245) at android.support.v7.widget.RecyclerView.setAdapter(RecyclerView.java:564) at com.sprintone.userInterface.Fragment.FirstFragment$1.successCallback(FirstFragment.java:101) at com.pubnub.api.PubnubCore.invokeCallback(Unknown Source) at com.pubnub.api.PubnubCore.invokeCallback(Unknown Source) at com.pubnub.api.PubnubCore$11.handleResponse(Unknown Source) at com.pubnub.api.NonSubscribeWorker.process(Unknown Source) at com.pubnub.api.Worker.run(Unknown Source) at java.lang.Thread.run(Thread.java:841)

yousef
  • 1,345
  • 1
  • 12
  • 20

1 Answers1

2

As stated here Only the original thread that created a view hierarchy can touch its views.

you can't access the ui element from outside the main thread try to use AsyncTask

or RunOnUiThread

Obviously that the PubNub Callbacks are works on background thread because in android you can't do operation like getting data from remote server on the main thread to avoid the app crash of the request fail and you are try to access the RecucleView from the pubnub callback
better to get the data in the pubnub callback and then set it in you view outside the callback

or you can delegation to get the date from pubnub callback when it's ready here some useful link link1
link2

Community
  • 1
  • 1
Antwan
  • 3,837
  • 9
  • 41
  • 62
  • so now you can get the data returned in the postexcute and then set it to the recycle view – Antwan Aug 02 '15 at 14:18
  • i used AsyncTask and set part PubNub CallBacks In method doInBackground .Obviously PubNub CallBacks work another thread and doInBackground finish work before PubNub Callbcks finish work hence i can't display result from PubNub CallBacks in method on postExecute at AsyncTask i want doInBackground Thread finish work at the same Time PubNub CallBacks finish work – yousef Aug 02 '15 at 14:29
  • in this case case you should use delegate to tell you when the pubnub callback finish then you can get the data in the delegate and pass it to the view – Antwan Aug 02 '15 at 17:42
  • can you give me one example for using delegate in this case ? – yousef Aug 02 '15 at 19:20
  • in the answer check it again – Antwan Aug 02 '15 at 20:01
  • i can't because i have only 11 reputation – yousef Aug 02 '15 at 20:10
  • i didn't say upvote you can see the links in the answer – Antwan Aug 02 '15 at 20:14
  • http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui do you tell me this link explain your delegate idea ? – yousef Aug 02 '15 at 20:36