2016-01-16 50 views
0

我正在使用搜索窗口小部件在我的主屏幕中自动完成。自动完成功能工作正常,但是当我选择任何建议应用程序崩溃与奇怪的NullPointerException。我花了足够的时间在Google上搜索,但无法找到任何线索。我正在粘贴下面的代码。Android Stack NullPointer在搜索窗口小部件中选择建议

@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; 
    } 

菜单项是

<item 
     android:id="@+id/menu_search" 
     android:icon="@drawable/searchicon" 
     android:title="@string/action_search" 
     android:showAsAction="always" 
     android:actionViewClass="android.widget.SearchView" 
     /> 

,并从日志中的例外是:

01-16 19:47:22.550 17995-17995/? E/AndroidRuntime: java.lang.NullPointerException 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.SearchView.launchSuggestion(SearchView.java:1387) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.SearchView.onItemClicked(SearchView.java:1303) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.SearchView.access$1800(SearchView.java:92) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.SearchView$9.onItemClick(SearchView.java:1327) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:931) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AutoCompleteTextView.access$400(AutoCompleteTextView.java:98) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1235) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AbsListView.performItemClick(AbsListView.java:1150) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.ListView.performItemClick(ListView.java:4397) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2985) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.widget.AbsListView$1.run(AbsListView.java:3671) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:615) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:155) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5520) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at java.lang.reflect.Method.invokeNative(Native Method) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:511) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825) 
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:  at dalvik.system.NativeStart.main(Native Method) 
01-16 19:47:22.560 399-4972/? E/EmbeddedLogger: App crashed! Process: com.example.app.myapplication 

但没有得到空指针,当我检查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; 
} 

有什么想法?

+0

@Henry NullPointer进入Android框架类 –

+0

我想你的'AutoSuggestionAdapter'返回null的东西。你的'建议列表'中有什么?要进行调试,您可以下载Android版本的源代码,并直接检查“SearchView.java”文件。 – Riaz

+0

Riaz我贴上了SearchView方法。在该行中不可能获得空指针。 –

回答

0

首先,代码中的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); 

在这里,从suggestionList适配器争夺的数据,但如果它的元素之一是null它可能导致NullPointerException

我不知道suggestionList是什么,但可以肯定它是由空值引起的。

+0

这是我的suggestionList..String [] suggestionList = {“Weight”,“Acceleration”,“lane”}; –

+0

另外,我用内置的ArrayAdapter测试了我的代码。同样的事情发生。我应该提供自己的自定义ContentProvider类吗? –

+0

@BhargavKumarR是的,请。如果没有空值,我猜自定义类可能会提供错误的计数/索引。 – WKBae

相关问题