GetSupportActionBar Error on Android

Asked 2 years ago, Updated 2 years ago, 39 views

An error (cannot resolve method) appears in the getSupportActionBar in the code below.
I wonder why.
Please let me know if you know more.

public class MainActivity extensions Activity {

    private WebView myWebView;
    privateEditText urlText;
    private static final String INITIAL_URL="http://doctormouse.me/";

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

        myWebView= (WebView) findViewById (R.id.myWebView);
        urlText=(EditText) findViewById (R.id.urlText);

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(newWebViewClient(){
            @ Override
            public void onPageFinished (WebView view, String url) {
                getSupportActionBar().setSubtitle(view.getTitle());
                urlText.setText(url);
            }
        });
        myWebView.loadUrl(INITIAL_URL);
    }

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

    @ Override
    public boolean onOptionsItemSelected(MenuItem){
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, solo
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        // no inspection SimplifiableIfStatement
        if(id==R.id.action_settings){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

java android

2022-09-29 21:56

1 Answers

getSupportActionBar() is the method used when using the support library, so

  • Change to getActionBar() without using the support library (but minSDKVersion>=11)
  • extendsAppCompatActivity instead of
  • extendsActivity (support-v7 must be added to the library)

Use one of the (although you will often end up using the latter AppCompatActivity)


2022-09-29 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.