每次运行代码时,应用程序崩溃它只有在我摆脱按钮点击事件时才有效,所以据我所知,问题出在 Btnclklistner 中事件
//package *******.*********;<<package not important
import android.app.Activity;
import android.os.Bundle;
//import android.view.Menu;<<i did this because i didnt really need to import them at the moment
//import android.view.MenuItem;<<
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
public class MyActivity extends Activity {
private WebView myWebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
myWebview = (WebView) findViewById(R.id.webView);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.loadUrl("http://clarkstoninfoboard.com/");
myWebview.getSettings().setLoadWithOverviewMode(true);
myWebview.getSettings().setUseWideViewPort(true);
myWebview.getSettings().setBuiltInZoomControls(true);
myWebview.getSettings().setDisplayZoomControls(false);
Btnclklistner();
}
private void Btnclklistner() {
Button WebRefresh = (Button) findViewById(R.id.btn_reld);
WebRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myWebview.reload();
}
});
}
例外:
FATAL EXCEPTION: main
Process: kirusoft.clarkstonupdates, PID: 7916 java.lang.RuntimeException: Unable to start activity ComponentInfo{kirusoft.clarkstonupdates/kirusoft.clarkstonupdates.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at kirusoft.clarkstonupdates.MyActivity.Btnclklistner(MyActivity.java:31)
at kirusoft.clarkstonupdates.MyActivity.onCreate(MyActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5473)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
布局XML
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reload"
android:textSize="10dp"
android:textColor="#939393"
android:id="@+id/btn_reld"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/webView"
android:layout_alignEnd="@+id/webView"
android:layout_above="@+id/webView" />