Edit : SOLVED, two xml files "individual_stats.xml" were present in the project.
After several pieces of research on the Internet, I haven't found any solution about my Activity crashing. The activity only contains a webview.
Moreover, I am new on Android Studio.
Here is the fatal error :
06-02 22:57:06.255 7618-7618/fr.berwick.smsstats E/AndroidRuntime: FATAL EXCEPTION: main
Process: fr.berwick.smsstats, PID: 7618
java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.berwick.smsstats/fr.berwick.smsstats.IndividualStats}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
at fr.berwick.smsstats.IndividualStats.onCreate(IndividualStats.java:19)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Please consider the following : when I add the webview to the layout (dragging it), ID filed is blank, so I named the WebView "WebView" and added android:id="@+id/WebView" to my activity xml file "individual_stats.xml".
Here is the code inside the IndividualStats.java file :
package fr.berwick.smsstats;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class IndividualStats extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.individual_stats);
WebView webview = (WebView) findViewById(R.id.WebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://speeload.com/");
}
}
Thank you for your help, have a great day.
尝试为xml中的webview赋予一个更唯一的名称?
try this :
final WebView webview = (WebView) findViewById(R.id.WebView);
webview.setWebViewClient(new WebViewClient());
WebSettings websetting = webview.getSettings();
websetting.setJavaScriptEnabled(true);
webview.loadUrl("https://speeload.com/");
Against which compileSdkVersion and minSdkVersion are you developing? On which API runs your device / emulator? Could be an older WebKit bug.
Okay I found out what was wrong :
I got two xml files named "individual_stats.xml". I deleted them and created a new one, it is now working.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.