gpt4 book ai didi

java - Android Searchview 导致 App 崩溃

转载 作者:行者123 更新时间:2023-11-29 21:05:13 25 4
gpt4 key购买 nike

感谢您查看这篇文章。我正在尝试在我的 android 应用程序操作栏中实现搜索 View (我将使用 Parse 来存储数据)。我已经关注了谷歌和我在网上找到的其他一些教程,但它不起作用。每次我尝试在 eclipse 中启动该应用程序时,它都会在不打开的情况下崩溃。(在没有搜索 View 之前它工作正常)。

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

ma​​in.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>

已删除 list

<?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" />

搜索结果 Activity :

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)

提前致谢

最佳答案

您正在使用:import android.support.v7.widget.SearchView; 它来自 Support Library 如果您查看 logcats,会说:

at com.example.trashed.MainActivity.onCreateOptionsMenu(MainActivity.java:55)

问题来自 MainActivity 和此方法 onCreateOptionsMenu

将您的搜索菜单替换为: 来自 documentation guide

<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />

如果您在导入中使用 v7,则不能像这样使用它:(android:actionViewClass="android.widget.SearchView")

<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"/>

documentation guide这样替换就好了.

此外,ActionBarActivity 已弃用,请改用 AppCompatActivity

代替:

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

}

尝试像这样使用它:

相反:

SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();

使用:

SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

重要的是:MenuItemCompat 因为您使用的是 v7 库。

也检查这个问题:

Android - NullPointerException on SearchView in Action Bar

关于java - Android Searchview 导致 App 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24818649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com