这是我的自定义网页视图
public class MycustomWebView extends WebView {
public static boolean sClicked = false;
private boolean mOverrideCheckIsTextEditor = true;
public View mCross;
// public MycustomWebView(Context context, AttributeSet attrs, int defStyle) {
// super(context, attrs, defStyle);
// }
public MycustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, android.R.attr.webViewStyle);
}
public MycustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MycustomWebView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
sClicked = true;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!hasFocus()) {
try {
requestFocus();
} catch(Exception e){
Toast.makeText(getContext(), "error no focus", Toast.LENGTH_SHORT).show();
Log.d("Focus issue", e.getMessage());
// Logger.v("can't get focus " + e.getMessage());
}
}
break;
}
try {
// crashes on some devices
return super.onTouchEvent(ev);
} catch(Exception e){
return false;
}
}
@Override
public boolean onCheckIsTextEditor() {
if (mOverrideCheckIsTextEditor) {
return true;
} else {
return super.onCheckIsTextEditor();
}
}
@Override
public void loadUrl(String url) {
super.loadUrl(url);
}
public void setOverrideOnCheckIsTextEditor(boolean b) {
mOverrideCheckIsTextEditor = b;
}
}
这是我的主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myweb1);
// myWebView= new WebView(this, null, android.R.attr.webViewStyle, true);
WebSettings webSettings = myWebView.getSettings();
myWebView.setFocusable(true);
myWebView.isFocusableInTouchMode();
myWebView.requestFocus();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://developer.samsung.com/resources.do");
}
}
这是我的 XML 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<com.example.testproj.MycustomWebView
android:id="@+id/myweb1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:layerType="software" />
<!-- <com.example.testproj.LiveWebView
android:id="@+id/myweb1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layerType="software" /> -->
</RelativeLayout>
我的问题是,当我在 HTC 设备中运行我的应用程序时,它会强制关闭,而在其他设备(例如 nexus)中它工作正常。任何人请帮助我
这是错误日志
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:9972)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)