gpt4 book ai didi

java - 将搜索查询从主 Activity 传递到 fragment

转载 作者:行者123 更新时间:2023-12-02 13:32:15 26 4
gpt4 key购买 nike

我正在使用 GIPHYAPI 构建一个用于 gif 搜索的 Android 应用程序,其中包含多个 fragment 和一个搜索菜单。每当我尝试传递查询时,它都会给出类似“尝试从空数组读取”的错误。
以下是我的 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)

我只想传递查询并加载适当的结果。

最佳答案

您从未初始化过您的 mFragments 数组,它看起来为空。

还有这一行,

((Searchable) mFragments[6]).performSearch(query);

您正在尝试到达空数组的第 6 个元素。

您的错误日志也说了同样的事情:

java.lang.NullPointerException: Attempt to read from null array

您必须初始化数组并将 fragment 添加到该数组中。

关于java - 将搜索查询从主 Activity 传递到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43140861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com