无法在可搜索Activity中通过搜索Intent显示PopupWindow问题排查
单Activity中搜索Intent已接收但PopupWindow无法显示问题
我开发的是单Activity(主Activity)应用,ActionBar中设有搜索按钮。主Activity已在清单文件中标记为可搜索Activity,且配置了Intent Filter拦截搜索Intent。
我希望在PopupWindow中展示搜索结果,已编写对应的Intent处理逻辑,Logcat显示能成功接收搜索Intent及查询字符串,但PopupWindow始终无法显示。请问问题出在哪里?是否遗漏了配置?恳请提供帮助。
主Activity代码
public class MainActivity extends AppCompatActivity { View popupView; PopupWindow popupWindow; ConstraintLayout constraintLayout; ArrayList foodList = DatabaseOpenHelper.foodList; Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constaint_layout); context = getApplicationContext(); AssetCopier assetCopier = new AssetCopier(this, "foods.db", "foods.db"); DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(this); databaseOpenHelper.getAllRecords(); MyAdapter adapter = new MyAdapter(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); Intent intent = getIntent(); handleIntent(intent); } public void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); Log.e("Message :","The search intent was received"); Log.e("Message :","The search query was: " + query); LayoutInflater inflater = LayoutInflater.from(context); popupView = inflater.inflate(R.layout.popup_window, null); TextView textBox = (TextView) popupView.findViewById(R.id.text_box); textBox.setText("This is the popup Window"); popupWindow = new PopupWindow(popupView); popupWindow.showAtLocation(constraintLayout, Gravity.CENTER, 0, 0); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); handleIntent(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.appbar_menu, menu); // associate searchable configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search_box).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); return super.onCreateOptionsMenu(menu); } }
PopupWindow布局文件代码
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="40dp" android:layout_marginTop="40dp" android:text="TextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者azeemofwalsall
相关产品推荐
相关产品推荐

