Android - searchview中的空指针异常

时间:2014-01-02 23:30:40

标签: java android eclipse android-fragments searchview

我在片段内部使用searchview并获得nullpointerexception。

我不明白为什么,我只想尝试查询...

我正在使用3标签片段,启动时应用程序崩溃

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>

你能告诉我我做错了什么,我怎么能成功?

1 个答案:

答案 0 :(得分:3)

您应该在片段视图中查找搜索视图,而不是在您的活动视图中。

更改

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

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