0

i m very new into android app. I m trying to execute an app that should contain a refresh button in the action bar. While compiling a code, the studio is prompting this error

"Error:(50, 20) error: non-static method reload() cannot be referenced from a static context"

I have written this uptil now

import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Menu;


public class MainActivity extends Activity {

private WebView mWebView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mWebView = new WebView(this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    final Activity activity = this;
    mWebView.setWebViewClient(new WebViewClient(){

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }

    });

    mWebView.loadUrl("http://www.example.com");
    setContentView(mWebView);

}

@Override
public boolean onCreateOptionsMenu (Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.action_refresh){
        WebView.reload();
        return true;
    }
    return super.onOptionsItemSelected(item);
 }
}

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"      tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />

 <item
    android:id="@+id/action_refresh"
    android:title="@string/refresh_button"
    android:icon="@drawable/ic_action_refresh"
    android:orderInCategory="1"
    app:showAsAction="never"
    />

</menu>
ashhad
  • 163
  • 1
  • 7
  • 18

1 Answers1

2

Change WebView.reload() to mWebView.reload()

In the former case, you are calling reload() on the WebView class, whereas in the latter you are telling your specific instance of WebView to reload.

This SO question may clarify things a bit more.

Community
  • 1
  • 1
bmat
  • 2,084
  • 18
  • 24
  • Thank you so much for the detailed explaination. It worked. However the refresh button is in the menu list, how can I make it visible as an icon? I have put into the main_menu.xml file – ashhad Feb 27 '15 at 23:42
  • In the line `app:showAsAction="never"`, change "never" to "always" or "ifRoom". Setting it to always will force the button to be shown, whereas ifRoom will show the button if there's enough room on the screen. – bmat Feb 27 '15 at 23:47