大家好我已经创建了一个带有自定义错误消息的WebView但是我无法运行它你们可以指导我在哪里出错吗
MainActivity.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.content.Context;
import android.widget.TextView;
public class MainActivity extends Activity {
private WebView nWebView;
float m_downX = 0;
WebView view;
ProgressBar progressBar;
TextView txtview;
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nWebView = (WebView) findViewById(R.id.webView1);
nWebView.getSettings().setJavaScriptEnabled(true);
nWebView.loadUrl("http://www.google.com/");
nWebView.setWebViewClient(new uosWebViewClient());
nWebView.setVerticalScrollBarEnabled(false);
nWebView.setHorizontalScrollBarEnabled(false);
ConnectivityDetector cd = new ConnectivityDetector(context);
Boolean isConnected = cd.isConnectedToInternet();
if (isConnected){
//do nothing
} else {
nWebView.setVisibility(View.GONE);
txtview.setText("Please connect to Internet");
txtview.setVisibility(View.VISIBLE);
}
nWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// save the x
m_downX = event.getX();
}
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
// set x so that it doesn't move
event.setLocation(m_downX, event.getY());
}
break;
}
return false;
}
});
}
private class uosWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
webview.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && nWebView.canGoBack()) {
nWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
ConectivityDetector
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectivityDetector extends MainActivity {
private Context ctxt;
public ConnectivityDetector(Context context) {
this.ctxt = context;
}
public boolean isConnectedToInternet() {
ConnectivityManager connectivityManager = (ConnectivityManager) ctxt.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo[] infos = connectivityManager.getAllNetworkInfo();
if (infos != null)
for (int i = 0; i < infos.length; i++)
if (infos[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
我同时提供了互联网权限和Acess网络状态权限
错误消息
enter code here03-13 11:06:55.775 1215-1215/com.just_like.just_like I/Process﹕ Sending signal. PID: 1215 SIG: 9
03-13 11:08:12.475 1296-1296/com.just_like.just_like V/WebViewChromium﹕ Binding Chromium to the background looper Looper (main, tid 1) {b3d19d38}
03-13 11:08:12.485 1296-1296/com.just_like.just_like I/chromium﹕ [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
03-13 11:08:12.495 1296-1296/com.just_like.just_like I/BrowserProcessMain﹕ Initializing chromium process, renderers=0
03-13 11:08:12.575 1296-1296/com.just_like.just_like E/chromium﹕ [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
03-13 11:08:12.575 1296-1296/com.just_like.just_like E/chromium﹕ [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
03-13 11:08:12.575 1296-1296/com.just_like.just_like E/chromium﹕ [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
03-13 11:08:12.575 1296-1296/com.just_like.just_like E/chromium﹕ [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
03-13 11:08:12.585 1296-1296/com.just_like.just_like E/chromium﹕ [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed
03-13 11:08:12.615 1296-1315/com.just_like.just_like W/chromium﹕ [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
03-13 11:08:12.725 1296-1296/com.just_like.just_like D/dalvikvm﹕ GC_FOR_ALLOC freed 88K, 6% free 2895K/3060K, paused 66ms, total 67ms
03-13 11:08:12.725 1296-1296/com.just_like.just_like I/dalvikvm-heap﹕ Grow heap (frag case) to 3.506MB for 635812-byte allocation
03-13 11:08:12.815 1296-1320/com.just_like.just_like D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 5% free 3516K/3684K, paused 86ms, total 86ms
03-13 11:08:12.985 1296-1296/com.just_like.just_like D/AndroidRuntime﹕ Shutting down VM
03-13 11:08:12.985 1296-1296/com.just_like.just_like W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a56ba8)
03-13 11:08:12.995 1296-1296/com.just_like.just_like E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.just_like.just_like, PID: 1296
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.just_like.just_like/com.just_like.just_like.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.just_like.just_like.ConnectivityDetector.isConnectedToInternet(ConnectivityDetector.java:16)
at com.just_like.just_like.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.just_like.just_like.ConnectivityDetector.isConnectedToInternet(ConnectivityDetector.java:16)
at com.just_like.just_like.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
答案 0 :(得分:0)
这是因为当您启动ConnectivityDetector cd = new ConnectivityDetector(context);
时,上下文为null。
尝试在该行之前添加context = this
。