質問

I am using a tabhost inside of a fragment, i have two tabs and i want to set text to a textview in a current tab from outside of this tabhost.

My layout looks like this:

I tried following:

  • Using fragments view -null exception
  • Inflating fragment in tabhost and using its view - null exception
  • Using tabhost.getCurrentTabView() - null exception

Last method gives me this error

FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.osembitdesign.xxxx.SearchActivity$6.onQueryTextSubmit(SearchActivity.java:111)
        at android.widget.SearchView.onSubmitQuery(SearchView.java:1204)
        at android.widget.SearchView.access$900(SearchView.java:93)
        at android.widget.SearchView$8.onEditorAction(SearchView.java:1179)
        at android.widget.TextView.onEditorAction(TextView.java:4216)
        at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)

I want to set text from a search view..

So how do i access tabhosts elements from outside?

EDIT:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //View v = inflater.inflate(R.layout.activity_search, container, false);
        final View rootView = inflater.inflate(R.layout.activity_search, container, false);

        mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("requests").setIndicator("Requests"),
                RequestTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("searchtab").setIndicator("Search"),
                SearchTab.class, null);


        SearchView srchView=(SearchView)rootView.findViewById(R.id.searchView);
        srchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                for(String i:testquery)
                {
                    if(i.contains(query))
                    {
                        Log.v("Search Result",i);
                        View tv=mTabHost.getCurrentTabView();
                        TextView listSearchResults=(TextView)tv.findViewById(R.id.textViewTestSearch);
                        listSearchResults.setText(i);
                    }
                }

                return false;
            }

        return rootView;
        //return v;
    }
役に立ちましたか?

解決

Ok i found a solution. Tabhosts get "current" whatever methods didn't work for me, so i did a workaround.

I fill bundle with data, send it back to the current tab and then "redraw"/reload current tab.

My code:

srchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                for(String i:testquery)
                {
                    if(i.contains(query))
                    {

                        //fill Bundle searchResults
                        searchResults.putString("rezultat", i);
                        //reload tabs
                        mTabHost.invalidate();
                        //i am switching my tabs, this refreshes UI
                        mTabHost.setCurrentTab(0);
                        mTabHost.setCurrentTab(1);

                    }
                }

                return false;
            }


        });

Fragment code where i update elements

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_search_tab, container, false);
    Bundle searchResults=getArguments();
    if(searchResults!=null)
    {
        TextView textView=(TextView)v.findViewById(R.id.textViewTestSearch);
        textView.setText(searchResults.getString("rezultat"));
    }

    return v;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top