2013-12-09 24 views
2

我有一個奇怪的事件。我有兩個手機4.1.2和2.3.6 Android版本,4.1.2工作正常,但較舊的一個拋出這個錯誤。java.lang.IllegalStateException:找不到方法

我的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> 

我公司主營:

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 

    } 

} 

但是,如果我用這個監聽器實現:

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

一切正常,可能有人解釋爲什麼出現這種情況?

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 
+0

如何發佈整個異常堆棧跟蹤? –

+0

http://developer.android.com/reference/android/R.attr.html#onClick API 4及以上 – Blundell

+0

@Blundell你的意思是什麼鏈接? 2,3,6是API 8及更高版本 – whiteLT

回答

2

How exactly does the android:onClick XML attribute differ from setOnClickListener?問題可能會對您有所幫助。看到最高的答案。它提到:

請注意,Android將僅在當前Activity中查找onClick方法。如果您使用的是Fragments,這一點很重要。因爲即使您使用片段添加上面的XML,Android將在而不是的片段的.java文件中查找onClick方法,用於添加XML片段。

希望這回答你爲什麼它不是第一種方式。

+0

是的,因爲他們都給出同樣的錯誤,我只複製粘貼其中的一個。 – whiteLT

+0

如果您可以請其他人發帖,可能會有所幫助。 –

+0

將在一秒內完成。 – whiteLT

相關問題