2012-10-12 23 views
1

我想给SearchableDictionary样品纳入我的应用程序有一个简单的词汇搜索能力。如果您不熟悉SearchableDictionary,请单击菜单中的搜索图标并输入搜索词。它建议根据您的搜索项目,如果你点击进入或者在键盘上输入这将拉动匹配搜索项的列表。选择一个项目将显示全屏定义。Android的 - 结合SerachableDictionary样品到我的应用程序

我已经incorperated这一点,它有一个例外的作品,当我打去还是之后只有几个字母在键盘上输入程序强制关闭,并不会带来了匹配项的列表。所有其他方面的工作,它建议在输入术语和点击一个项目的项目将提出其定义。

下面是当错误发生的logcat的。我发现它发生在SearchableDictionary.java中,但无法查明错误。

TL;博士 - 应用程序试图列出可能的搜索匹配时崩溃。

10-12 02:35:41.450: E/AndroidRuntime(524): FATAL EXCEPTION: main 
10-12 02:35:41.450: E/AndroidRuntime(524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.appname/com.company.appname.SearchableDictionary}: java.lang.NullPointerException 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.access$600(ActivityThread.java:122) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.os.Looper.loop(Looper.java:137) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.main(ActivityThread.java:4340) 
10-12 02:35:41.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invokeNative(Native Method) 
10-12 02:35:41.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invoke(Method.java:511) 
10-12 02:35:41.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-12 02:35:41.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-12 02:35:41.450: E/AndroidRuntime(524): at dalvik.system.NativeStart.main(Native Method) 
10-12 02:35:41.450: E/AndroidRuntime(524): Caused by: java.lang.NullPointerException 
10-12 02:35:41.450: E/AndroidRuntime(524): at com.company.appname.SearchableDictionary.showResults(SearchableDictionary.java:112) 
10-12 02:35:41.450: E/AndroidRuntime(524): at com.company.appname.SearchableDictionary.handleIntent(SearchableDictionary.java:78) 
10-12 02:35:41.450: E/AndroidRuntime(524): at com.company.appname.SearchableDictionary.onCreate(SearchableDictionary.java:56) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.Activity.performCreate(Activity.java:4465) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 
10-12 02:35:41.450: E/AndroidRuntime(524): ... 11 more 

而且,这里是全SearchableDictionary.java:

public class SearchableDictionary extends Activity { 

private TextView mTextView; 
private ListView mListView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search); 

    mTextView = (TextView) findViewById(R.id.text); 
    mListView = (ListView) findViewById(R.id.list); 

    handleIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    // Because this activity has set launchMode="singleTop", the system calls this method 
    // to deliver the intent if this actvity is currently the foreground activity when 
    // invoked again (when the user executes a search from this activity, we don't create 
    // a new instance of this activity, so the system delivers the search intent here) 
    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 
    if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
     // handles a click on a search suggestion; launches activity to show word 
     Intent wordIntent = new Intent(this, WordActivity.class); 
     wordIntent.setData(intent.getData()); 
     startActivity(wordIntent); 
     finish(); 
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     // handles a search query 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     showResults(query); 
    } 
} 

/** 
* Searches the dictionary and displays results for the given query. 
* @param query The search query 
*/ 
private void showResults(String query) { 

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null, 
          new String[] {query}, null); 

    if (cursor == null) { 
     // There are no results 
     mTextView.setText(getString(R.string.no_results, new Object[] {query})); 
    } else { 
     // Display the number of results 
     int count = cursor.getCount(); 
     String countString = getResources().getQuantityString(R.plurals.search_results, 
           count, new Object[] {count, query}); 
     mTextView.setText(countString); 

     // Specify the columns we want to display in the result 
     String[] from = new String[] { DictionaryDatabase.KEY_WORD, 
             DictionaryDatabase.KEY_DEFINITION }; 

     // Specify the corresponding layout elements where we want the columns to go 
     int[] to = new int[] { R.id.word, 
           R.id.definition }; 

     // Create a simple cursor adapter for the definitions and apply them to the ListView 
     SimpleCursorAdapter words = new SimpleCursorAdapter(this, 
             R.layout.result, cursor, from, to); 
     mListView.setAdapter(words); 

     // Define the on-click listener for the list items 
     mListView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // Build the Intent used to open WordActivity with a specific word Uri 
       Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 
       Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, 
               String.valueOf(id)); 
       wordIntent.setData(data); 
       startActivity(wordIntent); 
      } 
     }); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.options_menu, menu); 

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); 
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    searchView.setIconifiedByDefault(false); 

    return true; 
} 



@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int itemId = item.getItemId(); 
    if (itemId == R.id.search) { 
     onSearchRequested(); 
    } else if (itemId == R.id.atcMainSearch) { 
     Intent atcGoHome = new Intent(SearchableDictionary.this, 
       mymainactivity.class); 
     startActivity(atcGoHome); 
    } else if (itemId == R.id.atcHelpSearch) { 
     Intent atcAboutWeb = new Intent(SearchableDictionary.this, 
       atcAboutWeb.class); 
     startActivity(atcAboutWeb); 
    } 
    return true; 
} 

} 

谢谢你的人谁可以帮助!

回答

0

一个简单而忽视了错误引起的这个问题。在试图融入这种功能的同时,我没有使用所有的布局,而是试图引用一个不存在的布局。一旦布局被添加到我的项目中,应用程序按预期工作。

相关问题