Java 在搜索小部件中选择建议的Android堆栈空指针
我正在使用搜索小部件作为自动完成在我的主屏幕。自动完成功能运行良好,但当我选择任何建议时,应用程序会因奇怪的NullPointerException而崩溃。我花了足够的时间在谷歌上搜索,但找不到任何线索。我正在粘贴下面的代码Java 在搜索小部件中选择建议的Android堆栈空指针,java,android,autosuggest,Java,Android,Autosuggest,我正在使用搜索小部件作为自动完成在我的主屏幕。自动完成功能运行良好,但当我选择任何建议时,应用程序会因奇怪的NullPointerException而崩溃。我花了足够的时间在谷歌上搜索,但找不到任何线索。我正在粘贴下面的代码 @Override public boolean onCreateOptionsMenu(Menu menu) { Log.e("Conversion", "OnceateOption menu started"); // Inf
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e("Conversion", "OnceateOption menu started");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// This is sets our icon as search icon on title bar
ImageView searchHintIcon = (ImageView) findViewById(searchView,
"android:id/search_button");
searchHintIcon.setImageResource(R.drawable.searchicon);
// Set the hint text color
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(id);
searchTextView.setTextColor(Color.BLACK);
// Set hint in search widget
searchView.setQueryHint("Enter Conversion");
Log.e("Conversion", "Before setting auto suggestions");
// Set the auto suggestions
ArrayList<String> list = new ArrayList<String>(Arrays.asList(suggestionList));
AutoSuggestionAdapter autoSuggestionAdapter = new AutoSuggestionAdapter(this,
R.layout.autosuggestion_layout, list);
//ArrayAdapter<String> autoSuggestionAdapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_dropdown_item_1line, suggestionList);
((AutoCompleteTextView) searchTextView).setAdapter(autoSuggestionAdapter);
((AutoCompleteTextView) searchTextView).setThreshold(3);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default
Log.e("Conversion", "Autosuggestions set");
return true;
}
但当我检查android类代码时,不可能得到空指针
private Intent createIntent(String action, Uri data, String extraData, String query,
int actionKey, String actionMsg) {
// Now build the Intent
Intent intent = new Intent(action);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// We need CLEAR_TOP to avoid reusing an old task that has other activities
// on top of the one we want. We don't want to do this in in-app search though,
// as it can be destructive to the activity stack.
if (data != null) {
intent.setData(data);
}
intent.putExtra(SearchManager.USER_QUERY, mUserQuery);
if (query != null) {
intent.putExtra(SearchManager.QUERY, query);
}
if (extraData != null) { /**** LINE NUMBER 1387 NULL POINTER IN THIS LINE ******************/
intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
}
if (mAppSearchData != null) {
intent.putExtra(SearchManager.APP_DATA, mAppSearchData);
}
if (actionKey != KeyEvent.KEYCODE_UNKNOWN) {
intent.putExtra(SearchManager.ACTION_KEY, actionKey);
intent.putExtra(SearchManager.ACTION_MSG, actionMsg);
}
intent.setComponent(mSearchable.getSearchActivity());
return intent;
}
有什么想法吗?首先,代码上的SearchView代码与测试设备的版本不同,因此行号不匹配。它应该出现在日志中所说的
launchSuggestion()
方法中,而不是您发布的createIntent()
问题不在android的框架代码中,它在处理提供的建议列表时崩溃,很可能遇到列表中的null
数据
根据您的代码,以下部分是建议列表:
// Set the auto suggestions
ArrayList<String> list = new ArrayList<String>(Arrays.asList(suggestionList));
AutoSuggestionAdapter autoSuggestionAdapter = new AutoSuggestionAdapter(this,
R.layout.autosuggestion_layout, list);
//设置自动建议
ArrayList=newArrayList(Arrays.asList(suggestionList));
AutoSuggestionaAdapter AutoSuggestionaAdapter=新的AutoSuggestionaAdapter(此,
R.layout.autosuggestion_布局,列表);
在这里,适配器从suggestionList
中获取数据,但如果其中一个元素为null
,则可能导致NullPointerException
我不知道suggestionList中有什么,但可以确定它是由其中的空值引起的。首先,代码上的SearchView代码不是测试设备版本的代码,因此行号不匹配。它应该出现在日志中所说的
launchSuggestion()
方法中,而不是您发布的createIntent()
问题不在android的框架代码中,它在处理提供的建议列表时崩溃,很可能遇到列表中的null
数据
根据您的代码,以下部分是建议列表:
// Set the auto suggestions
ArrayList<String> list = new ArrayList<String>(Arrays.asList(suggestionList));
AutoSuggestionAdapter autoSuggestionAdapter = new AutoSuggestionAdapter(this,
R.layout.autosuggestion_layout, list);
//设置自动建议
ArrayList=newArrayList(Arrays.asList(suggestionList));
AutoSuggestionaAdapter AutoSuggestionaAdapter=新的AutoSuggestionaAdapter(此,
R.layout.autosuggestion_布局,列表);
在这里,适配器从suggestionList
中获取数据,但如果其中一个元素为null
,则可能导致NullPointerException
我不知道
suggestionList
中有什么,但可以确定它是由其中的null引起的。@Henry NullPointer出现在android框架classI中,我猜您的AutoSuggestionAdapter
为某些内容返回null。您的建议列表中有什么?为了确保调试,您可以下载您正在使用的Android版本的源代码,并直接检查SearchView.java
文件.Riaz I粘贴了上面的SearchView方法。不可能在该行中获得空指针。@Henry NullPointer将出现在android framework classI中,我猜想您的AutoSuggestionaAdapter
将返回空值。您的建议列表中有什么?为了确保调试,您可以下载您正在使用的Android版本的源代码,并直接检查SearchView.java
文件.Riaz I粘贴了上面的SearchView方法。这是我的suggestionList..String[]suggestionList={“权重”、“加速度”、“车道”};此外,我还使用内置的ArrayAdapter测试了我的代码。同样的事情发生了。我应该提供自己的自定义ContentProvider类吗?@BhargavKumarR是的,请。如果没有空值,我猜自定义类可能提供了错误的计数/索引。这是我的suggestionList..String[]suggestionList={“权重”、“加速度”、“车道”};此外,我还使用内置的ArrayAdapter测试了我的代码。同样的事情发生了。我应该提供自己的自定义ContentProvider类吗?@BhargavKumarR是的,请。如果没有空值,我猜自定义类可能提供了错误的计数/索引。
// Set the auto suggestions
ArrayList<String> list = new ArrayList<String>(Arrays.asList(suggestionList));
AutoSuggestionAdapter autoSuggestionAdapter = new AutoSuggestionAdapter(this,
R.layout.autosuggestion_layout, list);