在mainactivity中声明接收器

时间:2018-03-06 05:55:07

标签: java android webview

我有webView这样的声明(MainActivity.java)

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity
{
    public static WebView webView;
    WebView webView = (WebView) findViewById(R.id.webview);        
    webView.postUrl(url, postData.getBytes());
}

接收者处理连接更改(BroadcastReceiver.java)

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionBroadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            MainActivity.webView.reload();
        } else {
            MainActivity.webView.reload();
        }    
    }
}

清单(AndroidManifest.xml)

<application
        android:allowBackup="true"
        android:icon="@drawable/logo_full"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".ConnectionBroadReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
        </receiver>
    </application>

因为“webView”是“公共静态WebView webView”;我不能从另一个类调用虚方法..

  

onBroadReceiver:java.lang.NullPointerException:尝试调用   null对象上的虚方法'void android.webkit.WebView.reload()'   参考

我该怎么办?

1 个答案:

答案 0 :(得分:2)

而不是在receiver中注册manifest,而是在Activity注册并传递interface以进行网络状态更改互动

<强> NetworkCallback.java

interface NetworkCallback{
  void onStateChange();
}

ConnectionBroadReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionBroadReceiver extends BroadcastReceiver {
   private NetworkCallback callback;

    public ConnectionBroadReceiver(NetworkCallback callback){
          this.callback = callback;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            callback.onStateChange();
        } else {
             callback.onStateChange();
        }    
    }
}

现在进入你的Ativity

public class MainActivity extends AppCompatActivity
{
    private WebView webView;

   public void onCreate(Bundle savedInstanceState){
    // you code to set content view
    webView = (WebView) findViewById(R.id.webview);        
    webView.postUrl(url, postData.getBytes());
    registerReceiver(new ConnectionBroadReceiver(new NetworkCallback(){
          @Override
          public void onStateChange(){
               webview.reload();
          }
     }),new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); 
  }
}