1

I am using a searchview inside fragment and getting a nullpointerexception.

I don't understand why, I'm just trying to get the query...

I'm using a 3 tab fragment and the app crash when launching

MainActivity.java

 public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Search", "Top", "Download", "Library" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
 }

SearchFragment.java

 public class SearchFragment extends Fragment {

    private SearchView searchView1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_search, container, false);

        searchView1 = (SearchView) getActivity().findViewById(R.id.searchView1);

        final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                // Do something
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                // Do something
                performSearch();
                return true;
            }
        };

        searchView1.setOnQueryTextListener(queryTextListener);

        return rootView;
    }

    public void performSearch(){
        Toast.makeText(getActivity(), "search performer", Toast.LENGTH_SHORT).show();
    }
 }

fragment_search.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" 
     android:background="#17df0d">

     <SearchView
         android:id="@+id/searchView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:iconifiedByDefault="false"
         android:queryHint="@string/search_hint" >

     </SearchView>

     <ListView
         android:id="@+id/listView1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_below="@+id/searchView1" >
     </ListView>

 </RelativeLayout>

Can you please tell me what i'm doing wrong and how could I succeed?

2
  • Please post logcat with the error Commented Jan 2, 2014 at 23:32
  • 3
    In your logcat it should tell you which line is causing the error, then tell us which line that is. Commented Jan 2, 2014 at 23:33

1 Answer 1

3

You should look for the search view in your fragment's view, not in your activity's view.

Change

searchView1 = (SearchView) getActivity().findViewById(R.id.searchView1);

to

searchView1 = (SearchView) rootView.findViewById(R.id.searchView1);
Sign up to request clarification or add additional context in comments.

1 Comment

Do you know if you have it as a menu Item why it would be showing up as a null error in a fragment here is a link to the question stackoverflow.com/questions/22136032/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.