Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-将SerachableDictionary示例合并到我的应用程序中_Java_Android_Search_Sample - Fatal编程技术网

Java Android-将SerachableDictionary示例合并到我的应用程序中

Java Android-将SerachableDictionary示例合并到我的应用程序中,java,android,search,sample,Java,Android,Search,Sample,我正在尝试将SearchableDictionary示例合并到我的应用程序中,以实现简单的词汇表搜索功能。如果您不熟悉SearchableDictionary,请单击菜单中的搜索图标并输入搜索词。它会根据您的搜索建议项目,如果您在键盘上单击go或enter,它会弹出一个与搜索匹配的项目列表。选择项目将显示全屏定义 我已经输入了这个,只有一个例外,当我只在几个字母后点击键盘上的go或enter键时,程序强制关闭,并且不会显示匹配术语的列表。所有其他方面都起作用,它在键入术语时会提示条目,单击条目将

我正在尝试将SearchableDictionary示例合并到我的应用程序中,以实现简单的词汇表搜索功能。如果您不熟悉SearchableDictionary,请单击菜单中的搜索图标并输入搜索词。它会根据您的搜索建议项目,如果您在键盘上单击go或enter,它会弹出一个与搜索匹配的项目列表。选择项目将显示全屏定义

我已经输入了这个,只有一个例外,当我只在几个字母后点击键盘上的go或enter键时,程序强制关闭,并且不会显示匹配术语的列表。所有其他方面都起作用,它在键入术语时会提示条目,单击条目将显示其定义

下面是错误发生时间的日志。我在SearchableDictionary.java中看到了这种情况,但无法确定错误

tl;dr-应用程序在尝试列出可能的搜索匹配项时崩溃

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

}
公共类SearchableDictionary扩展活动{
私有文本视图mTextView;
私有列表视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mTextView=(TextView)findviewbyd(R.id.text);
mListView=(ListView)findViewById(R.id.list);
handleIntent(getIntent());
}
@凌驾
受保护的void onNewIntent(意图){
//由于此活动已设置launchMode=“singleTop”,系统将调用此方法
//如果此活动当前是前台活动,则在
//再次调用(当用户从此活动执行搜索时,我们不会创建
//此活动的新实例,因此系统在此传递搜索意图)
手册内容(意图);
}
私人无效手册内容(意图){
if(Intent.ACTION_VIEW.equals(Intent.getAction())){
//处理对搜索建议的单击;启动活动以显示word
Intent-wordIntent=newintent(这个,WordActivity.class);
setData(intent.getData());
startActivity(wordIntent);
完成();
}else if(Intent.ACTION_SEARCH.equals(Intent.getAction())){
//处理搜索查询
String query=intent.getStringExtra(SearchManager.query);
显示结果(查询);
}
}
/**
*搜索字典并显示给定查询的结果。
*@param query搜索查询
*/
私有void显示结果(字符串查询){
Cursor Cursor=managedQuery(DictionaryProvider.CONTENT_URI,null,null,
新字符串[]{query},null);
if(游标==null){
//没有结果
setText(getString(R.string.no_results,新对象[]{query}));
}否则{
//显示结果的数量
int count=cursor.getCount();
String countString=getResources().getQuantityString(R.plurals.search\u结果,
计数,新对象[]{count,query});
mTextView.setText(countString);
//指定要在结果中显示的列
String[]from=新字符串[]{DictionaryDatabase.KEY\u单词,
DictionaryDatabase.KEY_DEFINITION};
//指定相应的布局元素,以便将列放在其中
int[]to=新int[]{R.id.word,
R.id.definition};
//为定义创建一个简单的游标适配器,并将其应用于ListView
SimpleCrsorAdapter words=新的SimpleCrsorAdapter(此,
R.layout.result、光标、from、to);
mListView.setAdapter(字);
//为列表项定义单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//构建用于打开具有特定单词Uri的WordActivity的意图
Intent-wordIntent=newintent(getApplicationContext(),WordActivity.class);
Uri数据=Uri.withAppendedPath(DictionaryProvider.CONTENT\u Uri,
字符串。valueOf(id));
setData(数据);
startActivity(wordIntent);
}
});
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。选项菜单,菜单);
SearchManager SearchManager=(SearchManager)getSystemService(Context.SEARCH\u服务);
SearchView SearchView=(SearchView)菜单.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName());
searchView.setIconifiedByDefault(false);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int itemId=item.getItemId();
if(itemId==R.id.search){
onSearchRequested();
}else if(itemId==R.id.atcMainSearch){
Intent atcGoHome=新Intent(SearchableDictionary.this,
mymain(活动类);
星触觉(atcGoHome);
}else if(itemId==R.id.atcHelpSearch){
Intent ATCABOWWEB=新Intent(SearchableDictionary.this,
atcaboweb.class);
星触觉(atcaboweb);
} 
返回true;
}
}

谢谢你的帮助

一个简单而被忽略的错误导致了这个问题。在尝试合并此功能时,我没有使用所有的布局,而是尝试引用一个不存在的布局。一旦布局被添加到我的项目中,应用程序就会按预期运行