NoSuchMethodError error for getActionBar() (API-8)

后端 未结 6 1959
醉酒成梦
醉酒成梦 2020-12-17 09:59

when i run the app it installs but then crashes, ecplise isnt telling there is anything wrong with my code. i think its a problem with my manifest...



        
相关标签:
6条回答
  • 2020-12-17 10:36

    Use getSupportActionBar available with support library v7 as described here on the 6. Retrieve the Action Bar section

    getSupportActionBar documentation

    0 讨论(0)
  • 2020-12-17 10:38

    java.lang.NoSuchMethodError: com.example.secondapp.MainActivity.getActionBar

    On your MainActivity class, you call getActionBar() which is not available to your application.

    Your android:minSdkVersion is set to 8 (API-8), which does not provide getActionBar() (only since API-11).

    You should use ActionBarSherlock for good backward compatibility, or set android:minSdkVersion but then all devices < API-11 won't be targeted.

    0 讨论(0)
  • 2020-12-17 10:50

    I faced this same issue before. I just replace

    getActionBar()
    

    with

    getSupportActionBar()
    
    0 讨论(0)
  • 2020-12-17 10:55

    You are probably running on a phone that is lower than API 11, which is when the method getActionBar was introduced. If you need to run on devices lower than API level 11, then you will need to guard against executing those calls that only exist on newer API levels, or else use a compatibility library such as ActionBarSherlock or Action Bar Compatiblity. (See this thread for a discussion of the differences between these two.)

    Change the android:minSdkVersion="8" to android:minSdkVersion="11" and all the newer API calls that you are making will light up as errors. This will make it easier to locate those parts of your code that need attention.

    0 讨论(0)
  • 2020-12-17 10:57

    This is because the getActionBar is a method of api 11, but you can do this:

    if (android.os.Build.VERSION.SDK_INT >= 11)
        getActionBar().setDisplayHomeAsUpEnabled(true); //example
    

    and in the activity class add this suppress Lint:

    @SuppressLint("NewApi")
    
    0 讨论(0)
  • 2020-12-17 11:01

    For api < 11 you have to use:

    getSupportActionBar();
    

    Algo make sure you are using appcompat, add this on build.gradle, the 19.+ should be your project target version.

    compile 'com.android.support:appcompat-v7:19.+'
    

    Also if you are using invalidateOptionsMenu() for navigation drawer use this instead:

    supportInvalidateOptionsMenu();
    
    0 讨论(0)
提交回复
热议问题