InvocationTargetException - Recycler View - Android绑定服务

时间:2016-08-25 10:55:56

标签: java android service android-recyclerview

您好我正在开发一个聊天应用程序。我有一个Recyclerview来显示可用的用户。

我正在使用绑定服务,当新用户加入我的服务时,会将详细信息推送到 MainFragment 类中的 receivedincommingchats 方法。

我希望更新我的Recyclerview时间,我会收到 receivedincommingchats 方法的新用户信息。我能够从服务中接收用户信息。

但是当我尝试从 receivedincommingchats 方法更新Recyclerview时,我收到错误。

 :: HubConnection - Error: java.lang.reflect.InvocationTargetException
 java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at microsoft.aspnet.signalr.client.hubs.HubProxy$1.run(HubProxy.java:158)
     at microsoft.aspnet.signalr.client.hubs.HubProxy$1.run(HubProxy.java:1)
     at microsoft.aspnet.signalr.client.hubs.Subscription.onReceived(Subscription.java:31)
     at microsoft.aspnet.signalr.client.hubs.HubProxy.invokeEvent(HubProxy.java:308)
     at microsoft.aspnet.signalr.client.hubs.HubConnection.onReceived(HubConnection.java:118)
     at microsoft.aspnet.signalr.client.transport.TransportHelper.processReceivedData(TransportHelper.java:96)
     at microsoft.aspnet.signalr.client.Connection.processReceivedData(Connection.java:733)
     at microsoft.aspnet.signalr.client.Connection.access$0(Connection.java:728)
     at microsoft.aspnet.signalr.client.Connection$10.onData(Connection.java:671)
     at microsoft.aspnet.signalr.client.transport.ServerSentEventsTransport$1.onResponse(ServerSentEventsTransport.java:104)
     at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java:82)
     at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.NullPointerException
     at zupportdesk.desk.zupport.chatsystem.fragment.MainFragment.receivedincommingchats(MainFragment.java:134)
at zupportdesk.desk.zupport.chatsystem.Services.SignalRService$3.recieveIncomingChat(SignalRService.java:121)
08-25 16:12:52.227 21843-22276/zupportdesk.desk.zupport.chatsystem W/System.err:    ... 14 more    

MainFragment.class

public class MainFragment extends Fragment {

    private boolean mSearchCheck;
    private static final String TEXT_FRAGMENT = "TEXT_FRAGMENT";

    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private IncommingChatsAdapter adapter;
    List<IncommingItomObject> items = new ArrayList<IncommingItomObject>();


    public static MainFragment newInstance(String text){
        MainFragment mFragment = new MainFragment();
        Bundle mBundle = new Bundle();
        mBundle.putString(TEXT_FRAGMENT, text);
        mFragment.setArguments(mBundle);
        return mFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        recyclerView = (RecyclerView) rootView.findViewById (R.id.recycler_view_chats);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(rootView.getContext()));
        layoutManager = new LinearLayoutManager(rootView.getContext());
        recyclerView.setLayoutManager(layoutManager);
        int country_cd = R.drawable.lk;

        items.add(new IncommingItomObject(country_cd, "name", "visitor"));
        items.add(new IncommingItomObject(country_cd, "Sathyabaman", "Vid123456789-15236544-542"));
        adapter = new IncommingChatsAdapter(getContext(), items);

        recyclerView.setAdapter(adapter);
        Log.d("recyclerview", "on create fragment");



        rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));       
        return rootView;        
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu, menu);

        //Select search item
        final MenuItem menuItem = menu.findItem(R.id.menu_search);
        menuItem.setVisible(true);

        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setQueryHint(this.getString(R.string.search));

        ((EditText) searchView.findViewById(R.id.search_src_text))
                .setHintTextColor(getResources().getColor(R.color.nliveo_white));
        searchView.setOnQueryTextListener(onQuerySearchView);

        menu.findItem(R.id.menu_add).setVisible(true);

        mSearchCheck = false;   
    }


    public void receivedincommingchats(RecieveIncomingchats recieveIncomingchats){

        int country_cd = 0;
        Log.d("MFrag_visitor_id:", recieveIncomingchats.getVisitorID());
        Log.d("MFrag_company_id:", recieveIncomingchats.getCompanyId());
        Log.d("MFrag_name:", recieveIncomingchats.getName());
        Log.d("MFrag_widget_id:", recieveIncomingchats.getWidgetId());
        Log.d("MFrag_country_code:", String.valueOf(recieveIncomingchats.getCountryCode()));

        String country = recieveIncomingchats.getCountryCode();
        String name = recieveIncomingchats.getName();
        String visitor = recieveIncomingchats.getVisitorID();
        country_cd = R.drawable.lk;


            items.add(new IncommingItomObject(country_cd, "From service", "Coming from service"));
            adapter = new IncommingChatsAdapter(getContext(), items);
            recyclerView.setAdapter(adapter);
            Log.d("recyclerview", "After results received from service");



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {

        case R.id.menu_add:
            Toast.makeText(getActivity(), R.string.add, Toast.LENGTH_SHORT).show();
            break;

        case R.id.menu_search:
            mSearchCheck = true;
            Toast.makeText(getActivity(), R.string.search, Toast.LENGTH_SHORT).show();
            break;
        }
        return true;
    }   

   private SearchView.OnQueryTextListener onQuerySearchView = new SearchView.OnQueryTextListener() {
       @Override
       public boolean onQueryTextSubmit(String s) {
           return false;
       }

       @Override
       public boolean onQueryTextChange(String s) {
           if (mSearchCheck){
               // implement your search here
           }
           return false;
       }
   };
}

每次收到来自绑定服务的方法的信息后,有人可以帮我更新Recyclerview。

1 个答案:

答案 0 :(得分:0)

尝试直接在项目列表中添加新数据,然后请求更新。 而不是

items.add(new IncommingItomObject(country_cd, "From service", "Coming from service"));
adapter = new IncommingChatsAdapter(getContext(), items);
recyclerView.setAdapter(adapter);

您可以在适配器中添加一个方法,如此

public void addItem(IncommingItomObject obj) {
        items.add(obj);
        notifyDataSetChanged();
    }

并在您收到的通知方法中调用