后退按钮上的非静态方法错误

时间:2014-06-29 21:07:27

标签: java android android-activity

我正在尝试向应用程序添加一个后退按钮但是当我添加代码时,我得到“非静态方法”canGoBack()无法从静态上下文引用“错误。我已经阅读了几篇有关此错误的堆栈文章,但未能解决它。有什么想法吗?

package com.test;


import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.PushService;

public class MainActivity extends Activity implements OnClickListener {

    private Button push;

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {         
            Toast.makeText(getApplicationContext(), "onReceive invoked!", Toast.LENGTH_LONG).show();
        }
    };

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

        PushService.setDefaultPushCallback(this, MainActivity.class);


        Button back = (Button) findViewById(R.id.back);

        WebView webView = (WebView) findViewById(R.id.webView1);;
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.xxxxx.xxxxxx.xxxxx.xxxx// ");
        webView.setWebViewClient(new WebViewClient());


        push = (Button)findViewById(R.id.senPushB);
        push.setOnClickListener(this);
    }
    private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (true);
        }
    }



    @Override
    public void onBackPressed()
    {
        if(WebView.canGoBack()){
            WebView.goBack();
        }else{
            super.onBackPressed();
        }
    }
    @Override
    public void onPause() {
        super.onPause();

        LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public void onResume() {
        super.onResume();

        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(MyCustomReceiver.intentAction));
    }

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


    @Override
    public void onClick(View v) {

        JSONObject obj;
        try {
            obj = new JSONObject();
            obj.put("alert", "hello!");
            obj.put("action", MyCustomReceiver.intentAction);
            obj.put("customdata","My message");

            ParsePush push = new ParsePush();
            ParseQuery query = ParseInstallation.getQuery();

            // Push the notification to Android users
            query.whereEqualTo("deviceType", "android");
            push.setQuery(query);
            push.setData(obj);
            push.sendInBackground(); 
        } catch (JSONException e) {

            e.printStackTrace();
        }
    }


}

2 个答案:

答案 0 :(得分:3)

尝试更改:

@Override
public void onBackPressed()
{
    if(WebView.canGoBack()){
        WebView.goBack();
    }else{
        super.onBackPressed();
    }
}

为:

@Override
public void onBackPressed()
{
    WebView webView = (WebView) findViewById(R.id.webView1);
    if(webView.canGoBack()){
        webView.goBack();
    }else{
        super.onBackPressed();
    }
}

<强>说明: 您应该为您正在使用的webview 实例致电canGoBack()goBack()(转到上一个视图)。这也是该方法在实例级别而不是在类级别(静态)

声明的原因

答案 1 :(得分:2)

canGoBack是一种实例(非静态)方法。它只能在WebView类的实例上调用。 WebView就是这个类。仅当函数是静态函数时,调用WebView.function()才有效。您需要获取WebView的实例并在其上调用它。

对于记录,静态方法和实例方法之间的区别 - 静态方法可能不使用任何非静态数据。实例方法可以。静态数据每个类只有1个副本。非静态数据每个类的实例有1个副本。