我一直在关注如何在Android操作栏中实现搜索小部件的this tutorial。我的主要活动中的代码没有任何错误,但是当我在手机上运行代码时,应用程序崩溃了。有什么帮助吗?
这是logcat显示的内容
03-03 21:18:38.763 15310-15310/com.example.easylauncher E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.easylauncher, PID: 15310
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
at com.example.easylauncher.HomeActivity.onCreateOptionsMenu(HomeActivity.java:72)
at android.app.Activity.onCreatePanelMenu(Activity.java:2845)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
03-03 21:18:40.483 15310-15310/com.example.easylauncher D/Process﹕ killProcess, pid=15310
03-03 21:18:40.493 15310-15310/com.example.easylauncher D/Process﹕ com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
这是我的主要活动类
public class HomeActivity extends ActionBarActivity implements SearchView.OnQueryTextListener {
TextView mSearchText;
ImageButton phoneButton;
ImageButton contactsButton;
ImageButton messagesButton;
ImageButton cameraButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("Apps");
getActionBar();
loadApps();
loadListView();
addClickListener();
addphoneButtonListener();
addmessagesButtonListener();
addcontactsButtonListener();
addcameraButtonListener();
mSearchText = new TextView(this);
mSearchText.setPadding(10, 10, 10, 10);
mSearchText.setText("test");
setContentView(mSearchText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actions, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
// The following callbacks are called for the SearchView.OnQueryChangeListener
public boolean onQueryTextChange(String newText) {
newText = newText.isEmpty() ? "" : "Query so far: " + newText;
mSearchText.setText(newText);
mSearchText.setTextColor(Color.GREEN);
return true;
}
public boolean onQueryTextSubmit (String query) {
//Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
mSearchText.setText("Searching for: " + query + "...");
mSearchText.setTextColor(Color.RED);
return true;
}
这是我的actions.xml我收到所有showAsAction部分的错误,告诉我“应该使用app:showAsAction”
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Action Bar Search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item android:id="@+id/action_add"
android:icon="@android:drawable/ic_menu_add"
android:title="Action Bar Add" />
<item android:id="@+id/action_edit"
android:icon="@android:drawable/ic_menu_edit"
android:showAsAction="always"
android:title="Action Bar Edit" />
<item android:id="@+id/action_share"
android:icon="@android:drawable/ic_menu_share"
android:title="Action Bar Share"
android:showAsAction="ifRoom" />
</menu>
答案 0 :(得分:0)
由于您正在使用支持包中的活动类(ActionBarActivity
),因此您还需要使用支持小部件。
此外,要访问支持库中的showAsAction
属性,您需要指定自定义命名空间。这是一个例子:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Action Bar Search"
app:actionViewClass="android.support.v7.widget.searchview"
app:showAsAction="ifRoom"/>
...
</menu>
注意:不要忘记在您的活动中导入正确的类:
import android.support.v7.widget.SearchView;
答案 1 :(得分:0)
我发现在调试模式下不会出现崩溃,但是如果在PROGUARD
打开的情况下打开包装,那么崩溃就会发生!
我找到了这个帮助!
-keep class android.support.v7.widget.SearchView { *; }