如何使用 webview 在线性布局或相对布局上设置按钮
How to set button on linearlayout or relativelayout with webview
我的应用程序的全部功能是我有一个检测视图,其中包含一个编辑文本作为第一个视图。当 edittext 文本更改时,它将转到我的 activity_main 视图。如果 activity_main 只包含 webview,则所有功能都可以。但我需要添加一些按钮。发生错误。
我想用 webview 设置一些按钮
just like this
但是当我 运行 我的应用程序时,错误出现了
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)
我试过使用relativelayout,还是出现同样的错误。
这是我的 activity_main.xml 代码。
<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>
这是我的java代码。(按钮功能部分已标记)
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);
}
}
答案很简单,你实现了两个 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);删除这个
我的应用程序的全部功能是我有一个检测视图,其中包含一个编辑文本作为第一个视图。当 edittext 文本更改时,它将转到我的 activity_main 视图。如果 activity_main 只包含 webview,则所有功能都可以。但我需要添加一些按钮。发生错误。
我想用 webview 设置一些按钮 just like this
但是当我 运行 我的应用程序时,错误出现了
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)
我试过使用relativelayout,还是出现同样的错误。 这是我的 activity_main.xml 代码。
<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>
这是我的java代码。(按钮功能部分已标记)
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);
}
}
答案很简单,你实现了两个 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);删除这个