My app full function is that I have a Detect view contains a edittext as first view. When the edittext text change, it will go to my activity_main view. If the activity_main only contain webview, all function is alright. But I need to add some button. The error happened.
I want to set some button with webview just like this
But when I run my app, the error appear
E/AndroidRuntime: FATAL EXCEPTION: main Process: e.peter.laucher_compal, PID: 10188 java.lang.RuntimeException: Unable to start activity ComponentInfo{e.peter.laucher_compal/e.peter.laucher_compal.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I have tried to use relativelayout, but the same error still happened. Here is my activity_main.xml code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_web"
android:text="Web"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_setting"
android:text="Setting"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_uninstall"
android:text="Uninstall"/>
</LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
And It's my java code.(the part of button function has been marked)
public class MainActivity extends AppCompatActivity {
//Main code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Perform web view
WebView webview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(webview);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.youtube.com");
setDesktopMode(webview, true);
//Web button click event
/*Button button_web = (Button)findViewById(R.id.button_web);
button_web.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(MainActivity.this, MainActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
});
Button button_setting = (Button)findViewById(R.id.button_setting);
//Uninstall button click event
Button button_uninstall = (Button)findViewById(R.id.button_uninstall);
button_uninstall.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:e.peter.laucher_compal"));
startActivity(intent);
}
});*/
}
public void setDesktopMode(WebView webView, boolean enabled){
String newUserAgent = webView.getSettings().getUserAgentString();
if(enabled){
try{
String ua = webView.getSettings().getUserAgentString();
String androidOSString = webView.getSettings().getUserAgentString().substring(ua.indexOf("("), ua.indexOf(")") + 1);
newUserAgent = webView.getSettings().getUserAgentString().replace(androidOSString, "(X11; Linux x86_64)");
} catch (Exception e){
e.printStackTrace();
}
} else {
newUserAgent = null;
}
webView.getSettings().setUserAgentString(newUserAgent);
webView.getSettings().setUseWideViewPort(enabled);
webView.getSettings().setLoadWithOverviewMode(enabled);
webView.reload();
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK){
Intent intent = new Intent();
intent.setClass(MainActivity.this, Detect.class);
startActivity(intent);
MainActivity.this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
The answer is simple you implemented two setContentView
//Perform web view
WebView webview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(webview);//remove this one
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.youtube.com");
setDesktopMode(webview, true);
setContentView(webview); Remove this one