我正在使用 GIPHYAPI 构建一个用于 gif 搜索的 android 应用程序,其中包含多个片段和一个搜索菜单。每当我尝试传递查询时,它都会出现“尝试从空数组读取”之类的错误。
以下是我的 mainactivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, TabLayout.OnTabSelectedListener{
private TabLayout tabLayout;
private GifAdapter adapter;
private ViewPager viewPager;
private Fragment[] mFragments;
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Trending"));
tabLayout.addTab(tabLayout.newTab().setText("Funny"));
tabLayout.addTab(tabLayout.newTab().setText("Happy"));
tabLayout.addTab(tabLayout.newTab().setText("Sad"));
tabLayout.addTab(tabLayout.newTab().setText("Minion"));
tabLayout.addTab(tabLayout.newTab().setText("Awesome"));
tabLayout.addTab(tabLayout.newTab().setText("Search"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items,menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(6); //Switch to SearchView
}
});
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
viewPager.setCurrentItem(6);
return ((Searchable) mFragments[6]).performSearch(query);
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
以下是我的 FragActivitySearch.java
public class FragActivitySearch extends Fragment implements Searchable {
private RecyclerView recyclerView;
private GifAdapter adapter;
protected ViewPager mViewPager;
private Fragment[] mFragments;
private String Query;
@Override
public boolean performSearch(String query) {
Query = query;
new GiphyTask(Helper.getGiphyQueryUrl(Query,
100, GiphyQueryBuilder.EndPoint.SEARCH, ""), new GiphyTask.Callback() {
@Override
public void onResponse(List<Gif> gifs) {
adapter.setGifs(gifs);
}
}).execute();
return true;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.activity_search, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
adapter = new GifAdapter(getActivity(), new ArrayList<Gif>());
recyclerView.setAdapter(adapter);
recyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
GifAdapter.GifViewHolder gifHolder = (GifAdapter.GifViewHolder) holder;
gifHolder.stopPlayback();
}
});
return rootView;
}
}
每当我尝试运行此代码时,它都会给我以下运行时错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.share.gifimage, PID: 8275
java.lang.NullPointerException: Attempt to read from null array
at com.share.gifimage.MainActivity.onQueryTextSubmit(MainActivity.java:91)
at android.support.v7.widget.SearchView.onSubmitQuery(SearchView.java:1242)
at android.support.v7.widget.SearchView$9.onEditorAction(SearchView.java:1219)
at android.widget.TextView.onEditorAction(TextView.java:4902)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:364)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我只想传递查询并加载适当的结果。