本文介绍了Android的搜索查看造成应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢寻找这个帖子。我试图实现一个搜索视图进入我的Android应用程序动作条(我将使用解析来存储数据)。我按照谷歌的,和其他一些教程我在网上发现,这是行不通的。每次我尝试启动应用程序在eclipse崩溃而不打开。(之前的工作不细的搜索查看)。
thanks for looking at this post. I am attempting to implement a search view into my android applications actionbar (I will be using Parse to store the data). I have followed google's, and few other tutorials i found online and it will not work. Every time i attempt to start the app in eclipse it crashes without opening.(it worked fine before without the searchview).
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
public class MainActivity extends ActionBarActivity {
Button SubmitNewGame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, "WQfd6X9BRu0KLpZlNoPjVVfm4AST7G9zaIy3jMUo",
"ErxchQ7kgdw5kbhOWF1oQNfyEfjzRza2G6sRfDfb");
SubmitNewGame = (Button) findViewById(R.id.btnSubmitNewGame);
SubmitNewGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
SubmitActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
case R.id.action_toprated:
// location found
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
main.xml中:
<menu 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"
tools:context="com.example.trashed.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_toprated"
android:icon="@drawable/ic_action_toprated"
android:title="@string/action_toprated"
app:showAsAction="always"/>
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
</menu>
已移除清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trashed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SubmitActivity"
android:label="@string/title_activity_submit"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.trashed.MainActivity" />
</activity>
<activity android:name=".SearchResultsActivity"
android:parentActivityName="com.example.trashed.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
Searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />
SearchResultsActivity:
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SearchResultsActivity extends Activity {
private TextView txtQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
txtQuery = (TextView) findViewById(R.id.txtQuery);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
/**
* Use this query to display search results like
* 1. Getting the data from SQLite and showing in listview
* 2. Making webrequest and displaying the data
* For now we just display the query only
*/
txtQuery.setText("Search Query: " + query);
}
}
}
错误日志:
07-18 02:05:13.803: D/AndroidRuntime(2307): Shutting down VM
07-18 02:05:13.803: W/dalvikvm(2307): threadid=1: thread exiting with uncaught exception (group=0xb3ad8ba8)
07-18 02:05:13.833: E/AndroidRuntime(2307): FATAL EXCEPTION: main
07-18 02:05:13.833: E/AndroidRuntime(2307): Process: com.example.trashed, PID: 2307
07-18 02:05:13.833: E/AndroidRuntime(2307): java.lang.NullPointerException
07-18 02:05:13.833: E/AndroidRuntime(2307): at com.example.trashed.MainActivity.onCreateOptionsMenu(MainActivity.java:55)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:233)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:200)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
07-18 02:05:13.833: E/AndroidRuntime(2307): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
07-18 02:05:13.833: E/AndroidRuntime(2307): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
07-18 02:05:13.833: E/AndroidRuntime(2307): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.view.Choreographer.doFrame(Choreographer.java:543)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.os.Handler.handleCallback(Handler.java:733)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.os.Looper.loop(Looper.java:136)
07-18 02:05:13.833: E/AndroidRuntime(2307): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 02:05:13.833: E/AndroidRuntime(2307): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 02:05:13.833: E/AndroidRuntime(2307): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 02:05:13.833: E/AndroidRuntime(2307): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 02:05:13.833: E/AndroidRuntime(2307): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 02:05:13.833: E/AndroidRuntime(2307): at dalvik.system.NativeStart.main(Native Method)
在此先感谢
推荐答案
您应该使用支持搜索查看的支持版本:
You should use support SearchView's support version:
<item android:id="@+id/action_search"
android:title="@string/search_title"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
这篇关于Android的搜索查看造成应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!