我已经为简单的网络浏览器编写了代码,但是当我点击按钮时,应用程序停止显示消息"不幸的是,应用程序已停止"。它向我展示了致命的例外。
我已经发布了logcat,java代码以及布局。这三个都在同一部分。
logcat的
--------- beginning of crash
01-08 11:31:04.144 2603-2603/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.dell.firstapp, PID: 2603
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
at com.example.dell.firstapp.MyBrowser.onClick(MyBrowser.java:54)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-08 11:31:04.175 1242-1935/? W/ActivityManager﹕ Force finishing activity 1 com.example.dell.firstapp/.MyBrowser
01-08 11:31:04.603 1242-2595/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-08 11:31:04.850 1242-2595/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-08 11:31:04.850 1242-2595/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9f1d71a0, error=EGL_SUCCESS
01-08 11:31:04.975 1242-1260/? I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
01-08 11:31:06.604 1242-1260/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{35c22eec u0 com.example.dell.firstapp/.MyBrowser t18 f}
01-08 11:31:06.614 1242-1260/? I/Choreographer﹕ Skipped 98 frames! The application may be doing too much work on its main thread.
01-08 11:31:14.222 1242-1260/? W/ActivityManager﹕ Launch timeout has expired, giving up wake lock!
public class MyBrowser extends Activity implements View.OnClickListener{
EditText url;
WebView ourBrow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simplebrowser);
WebView ourBrow=(WebView) findViewById(R.id.wvBrowser);
ourBrow.getSettings().setJavaScriptEnabled(true);
ourBrow.getSettings().setLoadWithOverviewMode(true);
ourBrow.getSettings().setUseWideViewPort(true);
ourBrow.setWebViewClient(new ourViewClient());
try {
ourBrow.loadUrl("http://www.google.com");
}catch (Exception e){
e.printStackTrace();
}
Button go=(Button) findViewById(R.id.bGo);
Button back=(Button) findViewById(R.id.bBack);
Button forward=(Button) findViewById(R.id.bFwd);
Button refresh=(Button) findViewById(R.id.bRefresh);
Button clearHistory=(Button) findViewById(R.id.bHistory);
url=(EditText) findViewById(R.id.etURL);
go.setOnClickListener(this);
back.setOnClickListener(this);
forward.setOnClickListener(this);
refresh.setOnClickListener(this);
clearHistory.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bGo:
String theWebsite=url.getText().toString();
ourBrow.loadUrl(theWebsite);
//Hiding the keyboard after using an EditText
InputMethodManager imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(url.getWindowToken(),0);
break;
case R.id.bBack:
if(ourBrow.canGoBack())
ourBrow.goBack();
break;
case R.id.bFwd:
if(ourBrow.canGoForward())
ourBrow.goForward();
break;
case R.id.bRefresh:
ourBrow.reload();
break;
case R.id.bHistory:
ourBrow.clearHistory();
break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:weightSum="100"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etURL"
android:layout_weight="20" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go"
android:id="@+id/bGo"
android:layout_weight="80"
android:layout_marginRight="16dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="8"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bBack"
android:text="Go back page"
android:layout_weight="2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bFwd"
android:text="Go forward"
android:layout_weight="2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bRefresh"
android:text="Refresh page"
android:layout_weight="2"/>
<Button android:text="clear History"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bHistory"
android:layout_weight="2"/>
</LinearLayout>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/wvBrowser"></WebView>
</LinearLayout>
答案 0 :(得分:1)
您获得此nullPointer的原因是您没有正确初始化您的webview。
您为全局WebView ourBrow
创建对象;并且您在onClick
方法ourBrow.loadUrl(theWebsite);
内使用相同内容。在这种情况下,您的webview仍为空。
在onCreate方法中,您正在使用带有以下代码的webview。
WebView ourBrow=(WebView) findViewById(R.id.wvBrowser);
所以在oncreate方法中你的代码没有得到nullPointer
。
解决方案:
只需改变这一点
WebView ourBrow=(WebView) findViewById(R.id.wvBrowser);
到
ourBrow=(WebView) findViewById(R.id.wvBrowser);
注意: ρяσѕρєяK已在评论中添加了解决方案。我只是添加解释。
答案 1 :(得分:0)
Please remove local declaration of webview object 'ourBrow' inside onCreate() method of activity. please replace your line 'WebView ourBrow=(WebView) findViewById(R.id.wvBrowser);' with 'ourBrow=(WebView) findViewById(R.id.wvBrowser);'.
When you declare local webview object than the local object got initialize and global object will still null. And inside of onClick() method global object will access so it is got every time null.