gpt4 book ai didi

android - 我的 android 应用程序强制关闭, toast 和操作栏搜索

转载 作者:行者123 更新时间:2023-11-29 17:59:44 24 4
gpt4 key购买 nike

我的应用程序只有一个操作栏搜索(不工作)成功加载到我的 android galaxy nexus 上。当我去实际执行搜索时,通过检索搜索并弹出 toast,它在启动时崩溃了。

我的 eclipse 错误提示显示:

06-04 14:33:06.902: D/libEGL(11017): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
06-04 14:33:06.956: D/libEGL(11017): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
06-04 14:33:06.987: D/libEGL(11017): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
06-04 14:33:07.112: D/OpenGLRenderer(11017): Enabling debug mode 0
06-04 14:33:07.237: D/AndroidRuntime(11017): Shutting down VM
06-04 14:33:07.237: W/dalvikvm(11017): threadid=1: thread exiting with uncaught exception (group=0x4121c930)
06-04 14:33:07.253: E/AndroidRuntime(11017): FATAL EXCEPTION: main
06-04 14:33:07.253: E/AndroidRuntime(11017): java.lang.ClassCastException: com.example.beerportfoliopro.MainActivity cannot be cast to android.widget.SearchView$OnQueryTextListener
06-04 14:33:07.253: E/AndroidRuntime(11017): at com.example.beerportfoliopro.MainActivity.onCreateOptionsMenu(MainActivity.java:25)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
06-04 14:33:07.253: E/AndroidRuntime(11017): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:460)
06-04 14:33:07.253: E/AndroidRuntime(11017): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:822)
06-04 14:33:07.253: E/AndroidRuntime(11017): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:253)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.view.Choreographer.doFrame(Choreographer.java:531)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.os.Handler.handleCallback(Handler.java:725)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.os.Looper.loop(Looper.java:137)
06-04 14:33:07.253: E/AndroidRuntime(11017): at android.app.ActivityThread.main(ActivityThread.java:5226)
06-04 14:33:07.253: E/AndroidRuntime(11017): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 14:33:07.253: E/AndroidRuntime(11017): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 14:33:07.253: E/AndroidRuntime(11017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-04 14:33:07.253: E/AndroidRuntime(11017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-04 14:33:07.253: E/AndroidRuntime(11017): at dalvik.system.NativeStart.main(Native Method)

我实现这个的主要地方是这样的:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setOnQueryTextListener((OnQueryTextListener) this);
return true;
}

//get entered search and pop up a toast to show query entered
public boolean onQueryTextSubmit (String query) {
Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();

return true;
}



}

最佳答案

searchView.setOnQueryTextListener((OnQueryTextListener) this);

在此行中,您正尝试将 Activity 转换到您的 OnQueryTextListener 类。这是完全错误的。

而不是发送一个监听器对象作为参数

关于android - 我的 android 应用程序强制关闭, toast 和操作栏搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16925046/

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