如何从创建tabcontent

时间:2017-04-04 07:28:41

标签: android

我正在尝试使用 tabhost制作标签式应用。所以问题是** onclick **添加到按钮,即通过 tabcontent 中的布局进行充气使用 createtab内容方法,无法正常工作。单击按钮时,应用程序崩溃。请看看我用过的代码。 这只是我创建的方法,用于动态添加选项卡的选项卡和内容

public void addtab(View v)
{
    tabno++;
    tabname = String.valueOf(tabno);
    tabnum++;
    TabHost mTabHost = (TabHost) findViewById(R.id.tabHost);
    mTabHost.setup();
    //Lets add the first Tab
    TabHost.TabSpec Spec = mTabHost.newTabSpec("tab");
    // Spec.setContent(R.id.layoutone);
    Spec.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            final LinearLayout layoutone = new LinearLayout(browser.this);
            layoutone.setId(R.id.layoutone);
            layoutone.setOrientation(LinearLayout.VERTICAL);
            View adrressvar =getLayoutInflater().inflate(R.layout.xcc,layoutone,false);
            search2=(Button) adrressvar.findViewById(R.id.search2);
            urltwo=(EditText)  adrressvar.findViewById(R.id.urlbar2);

            search2.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    wb.loadUrl(keyword+urltwo.getText().toString());
                }
            });

            LinearLayout.LayoutParams wbprm= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            wb= new WebView(browser.this);
            wb.setWebViewClient(new WebViewClient());

            wb.loadUrl(ty);

            wb.setId(R.id.wbdy);

            layoutone.addView(adrressvar);
            layoutone.addView(wb,wbprm);
            return layoutone;
        }
    });

    Spec.setIndicator("TAB" + tabname);
    mTabHost.addTab(Spec);
    mTabHost.setCurrentTab(tabnum);
}

logcat错误 04-04 14:21:23.722 14715-14715 / com.example.lenovo1.tabhostbrowser E / AndroidRuntime:FATAL EXCEPTION:main                                                                                     处理:com.example.lenovo1.tabhostbrowser,PID:14715                                                                                     java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。                                                                                         在android.view.ViewGroup.addViewInner(ViewGroup.java:4417)                                                                                         在android.view.ViewGroup.addView(ViewGroup.java:4258)                                                                                         在android.view.ViewGroup.addView(ViewGroup.java:4198)                                                                                         在android.view.ViewGroup.addView(ViewGroup.java:4171)                                                                                         在com.example.lenovo1.tabhostbrowser.browser $ 1 $ 1.onClick(browser.java:114)                                                                                         在android.view.View.performClick(View.java:5637)                                                                                         在android.view.View $ PerformClick.run(View.java:22429)                                                                                         在android.os.Handler.handleCallback(Handler.java:751)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                         在android.os.Looper.loop(Looper.java:154)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:0)

您是否在按钮点击时使用方法eval之前创建了WebView的实例?与loadUrl

一样

如果是 - 请确保您传入new WebView();的内容不是loadUrl

我目前无法提供更多信息,因为您没有提供应用崩溃时发生的任何错误消息。