java.lang.IllegalStateException:找不到的方法 [英] java.lang.IllegalStateException: Could not find a method
问题描述
我有一个奇怪的事件。我有两个手机4.1.2和2.3.6的Android版本,4.1.2工作正常,但年长之一抛出此错误。
我的XML:
<?XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的ImageButton 机器人:ID =@ + ID / IB_Nearest
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =IB_Nearest
机器人:layout_alignParentTop =真
机器人:layout_alignParentLeft =真
机器人:SRC =@绘制/标记
机器人:背景=@空
机器人:layout_margin =30dp
/>
<的ImageButton
机器人:ID =@ + ID / IB_Category
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =IB_Category
机器人:SRC =@绘制/书
机器人:layout_alignParentTop =真
机器人:layout_alignParentRight =真
机器人:背景=@空
机器人:layout_margin =30dp
/>
<的ImageButton 机器人:ID =@ + ID / IB_Map
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =IB_Map
机器人:layout_below =@ ID / IB_Category
机器人:layout_alignParentRight =真
机器人:SRC =@绘制/ mappp
机器人:背景=@空
机器人:layout_margin =30dp
/>
<的ImageButton
机器人:ID =@ + ID / IB_Search
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =IB_Search
机器人:SRC =@绘制/搜索
机器人:layout_below =@ ID / IB_Nearest
机器人:layout_alignParentLeft =真
机器人:背景=@空
机器人:layout_margin =30dp
/>
<的ImageButton 机器人:ID =@ + ID / IB_NewPlace
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =IB_NewPlace
机器人:layout_below =@ ID / IB_Search
机器人:layout_alignParentLeft =真
机器人:SRC =@绘制/信息
机器人:背景=@空 /><的ImageButton
机器人:ID =@ + ID / IB_Statistics
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =IB_Statistics
机器人:SRC =@绘制/图
机器人:layout_below =@ ID / IB_Map
机器人:layout_alignParentRight =真
机器人:背景=@空 />
< / RelativeLayout的>
< /滚动型>
我公司主营:
公共类MainActivity扩展FragmentActivity工具
LoaderManager.LoaderCallbacks<&光标GT; { @覆盖
保护无效的onCreate(捆绑savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
}
公共无效IB_Map(视图v){
意向意图=新意图(MainActivity.this,Map.class);
startActivity(意向);
}
公共无效IB_Category(视图v){ 意向意图=新意图(MainActivity.this,TreeChoice.class);
startActivity(意向);
}
公共无效IB_Nearest(视图v){
意向意图=新意图(MainActivity.this,NearPlaces.class);
startActivity(意向);
}
公共无效IB_Search(视图v){ 意向意图=新意图(MainActivity.this,Search.class);
startActivity(意向);
}
公共无效IB_NewPlace(视图v){
意向意图=新意图(MainActivity.this,NewPlace.class);
startActivity(意向);
}
公共无效IB_Statistics(视图v){ 意向意图=新意图(MainActivity.this,Statistics.class);
startActivity(意向);
}
/ **
*背景异步任务以通过HTTP请求负载的所有产品
* * /
公共类LoadAllProducts扩展的AsyncTask<字符串,字符串,字符串> { / **
*启动后台线程显示进度对话框之前
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute(); } / **
*让所有的产品网址
* * /
保护字符串doInBackground(字符串参数... args){ 返回null;
} / **
*在完成后台任务之后辞退进度对话框
* ** /
保护无效onPostExecute(字符串FILE_URL){ } } @覆盖
公共android.support.v4.content.Loader<&光标GT; onCreateLoader(INT为arg0,ARG1捆绑){
// TODO自动生成方法存根
返回null;
}
公共无效onLoadFinished(装载机<&光标GT;为arg0,ARG1光标){
// TODO自动生成方法存根 }
公共无效onLoaderReset(装载机<&光标GT;为arg0){
// TODO自动生成方法存根 }
@覆盖
公共无效onLoadFinished(
android.support.v4.content.Loader<&光标GT;为arg0,ARG1光标){
// TODO自动生成方法存根 }
@覆盖
公共无效onLoaderReset(android.support.v4.content.Loader<&光标GT;为arg0){
// TODO自动生成方法存根 }}
但是,如果我用这个监听器实现:
最后的ImageButton rightButton =(的ImageButton)findViewById(R.id.IB_Map);
rightButton.setOnClickListener(新ImageButton.OnClickListener(){
公共无效的onClick(视图v)
{
意向意图=新意图(MainActivity.this,Map.class);
startActivity(意向);
}
});
一切正常,可能有人解释为什么出现这种情况?
12-10 00:05:21.875:E / AndroidRuntime(28537):致命异常:主要
12-10 00:05:21.875:E / AndroidRuntime(28537):java.lang.IllegalStateException:找不到在活动课lt.whitegroup.workplaces.ui.MainActivity为的onClick处理程序上的视图类中的方法IB_Map(视图) android.widget.ImageButton ID为IB_Map
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.view.View $ 1.onClick(View.java:2131)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.view.View.performClick(View.java:2485)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.view.View $ PerformClick.run(View.java:9080)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.os.Handler.handleCallback(Handler.java:587)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.os.Handler.dispatchMessage(Handler.java:92)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.os.Looper.loop(Looper.java:130)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.app.ActivityThread.main(ActivityThread.java:3687)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.reflect.Method.invokeNative(本机方法)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.reflect.Method.invoke(Method.java:507)
12-10 00:05:21.875:E / AndroidRuntime(28537):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)
12-10 00:05:21.875:E / AndroidRuntime(28537):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-10 00:05:21.875:E / AndroidRuntime(28537):在dalvik.system.NativeStart.main(本机方法)
12-10 00:05:21.875:E / AndroidRuntime(28537):由:java.lang.NoSuchMethodException
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.Class.getDeclaredMethods(本机方法)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.ClassCache.getDeclaredPublicMethods(ClassCache.java:153)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.ClassCache.getMethodsRecursive(ClassCache.java:216)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.ClassCache.findMethods(ClassCache.java:175)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.ClassCache.getMethods(ClassCache.java:167)
12-10 00:05:21.875:E / AndroidRuntime(28537):在java.lang.Class.getMethod(Class.java:961)
12-10 00:05:21.875:E / AndroidRuntime(28537):在android.view.View $ 1.onClick(View.java:2124)
12-10 00:05:21.875:E / AndroidRuntime(28537):... 11个
<一个href=\"http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene\">How究竟android的:的onClick XML属性从setOnClickListener不同的问题可以帮助你?看到最upvoted答案。它mentiones:
请注意,Android会寻找
的onClick
方法仅在当前活动。这要记住,如果你正在使用片段是非常重要的
。因为即使你加利用一个片段XML,Android将不会寻找在用来片段的.java文件中的的onClick
方法添加XML。
块引用>希望这回答了你,为什么它不工作的第一种方式。
I have a strange event. I have two phones 4.1.2 and 2.3.6 android versions, 4.1.2 works fine but older one throws this error.
My XML:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageButton android:id="@+id/IB_Nearest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="IB_Nearest" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:src="@drawable/marker" android:background="@null" android:layout_margin="30dp" /> <ImageButton android:id="@+id/IB_Category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="IB_Category" android:src="@drawable/book" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:background="@null" android:layout_margin="30dp" /> <ImageButton android:id="@+id/IB_Map" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="IB_Map" android:layout_below="@id/IB_Category" android:layout_alignParentRight="true" android:src="@drawable/mappp" android:background="@null" android:layout_margin="30dp" /> <ImageButton android:id="@+id/IB_Search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="IB_Search" android:src="@drawable/search" android:layout_below="@id/IB_Nearest" android:layout_alignParentLeft="true" android:background="@null" android:layout_margin="30dp" /> <ImageButton android:id="@+id/IB_NewPlace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="IB_NewPlace" android:layout_below="@id/IB_Search" android:layout_alignParentLeft="true" android:src="@drawable/info" android:background="@null" /> <ImageButton android:id="@+id/IB_Statistics" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="IB_Statistics" android:src="@drawable/chart" android:layout_below="@id/IB_Map" android:layout_alignParentRight="true" android:background="@null" /> </RelativeLayout> </ScrollView>
My Main:
public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{ @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); } public void IB_Map (View v){ Intent intent = new Intent(MainActivity.this, Map.class); startActivity(intent); } public void IB_Category(View v){ Intent intent = new Intent(MainActivity.this, TreeChoice.class); startActivity(intent); } public void IB_Nearest (View v){ Intent intent = new Intent(MainActivity.this, NearPlaces.class); startActivity(intent); } public void IB_Search (View v){ Intent intent = new Intent(MainActivity.this, Search.class); startActivity(intent); } public void IB_NewPlace (View v){ Intent intent = new Intent(MainActivity.this, NewPlace.class); startActivity(intent); } public void IB_Statistics (View v){ Intent intent = new Intent(MainActivity.this, Statistics.class); startActivity(intent); } /** * Background Async Task to Load all product by making HTTP Request * */ public class LoadAllProducts extends AsyncTask<String, String, String> { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); } /** * getting All products from url * */ protected String doInBackground(String... args) { return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { } } @Override public android.support.v4.content.Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { // TODO Auto-generated method stub return null; } public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) { // TODO Auto-generated method stub } public void onLoaderReset(Loader<Cursor> arg0) { // TODO Auto-generated method stub } @Override public void onLoadFinished( android.support.v4.content.Loader<Cursor> arg0, Cursor arg1) { // TODO Auto-generated method stub } @Override public void onLoaderReset(android.support.v4.content.Loader<Cursor> arg0) { // TODO Auto-generated method stub } }
But if I use this listener implementation:
final ImageButton rightButton = (ImageButton) findViewById(R.id.IB_Map); rightButton.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Map.class); startActivity(intent); } });
Everything works, could someone explain why this happens?
12-10 00:05:21.875: E/AndroidRuntime(28537): FATAL EXCEPTION: main 12-10 00:05:21.875: E/AndroidRuntime(28537): java.lang.IllegalStateException: Could not find a method IB_Map(View) in the activity class lt.whitegroup.workplaces.ui.MainActivity for onClick handler on view class android.widget.ImageButton with id 'IB_Map' 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.view.View$1.onClick(View.java:2131) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.view.View.performClick(View.java:2485) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.view.View$PerformClick.run(View.java:9080) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.os.Handler.handleCallback(Handler.java:587) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.os.Handler.dispatchMessage(Handler.java:92) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.os.Looper.loop(Looper.java:130) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.app.ActivityThread.main(ActivityThread.java:3687) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.reflect.Method.invokeNative(Native Method) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.reflect.Method.invoke(Method.java:507) 12-10 00:05:21.875: E/AndroidRuntime(28537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 12-10 00:05:21.875: E/AndroidRuntime(28537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 12-10 00:05:21.875: E/AndroidRuntime(28537): at dalvik.system.NativeStart.main(Native Method) 12-10 00:05:21.875: E/AndroidRuntime(28537): Caused by: java.lang.NoSuchMethodException 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.Class.getDeclaredMethods(Native Method) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.ClassCache.getDeclaredPublicMethods(ClassCache.java:153) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.ClassCache.getMethodsRecursive(ClassCache.java:216) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.ClassCache.findMethods(ClassCache.java:175) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.ClassCache.getMethods(ClassCache.java:167) 12-10 00:05:21.875: E/AndroidRuntime(28537): at java.lang.Class.getMethod(Class.java:961) 12-10 00:05:21.875: E/AndroidRuntime(28537): at android.view.View$1.onClick(View.java:2124) 12-10 00:05:21.875: E/AndroidRuntime(28537): ... 11 more
解决方案How exactly does the android:onClick XML attribute differ from setOnClickListener? question might help you. See the most upvoted answer. It mentiones:
Note that Android will look for the
onClick
method only in the current Activity. This is important to remember if you are usingFragments
. Since even if you add the XML above using a fragment, Android will not look for theonClick
method in the .java file of the fragment used to add the XML.Hope this answers you why it is not working the first way.
这篇关于java.lang.IllegalStateException:找不到的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!