如何在选项卡中使用WebView并调用“(LocalActivityManager activityGroup)”?

时间:2013-05-10 05:09:14

标签: android android-activity android-webview android-tabhost android-logcat

我想使用三个标签来利用我的三个网页浏览来显示特定的域。一切都建立了发现,但当我尝试启动应用程序时,它强制关闭,当我运行它&如果我调试它 - 它使用布局和视图保持打开(它在调试时基本上什么都不做。)

logcat说

  

无法启动活动ComponentInfo {com.company.client / com.company.client.MainActivity}:java.lang.IllegalStateException:您是否忘记调用'public void setup(LocalActivityManager activityGroup)'?**

我怎么称呼它?

继承我的代码:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {
TabHost th;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    th = (TabHost) findViewById(R.id.tabhost);
    th.setup();


    TabSpec firstSpec=th.newTabSpec("Classes & Events");
    firstSpec.setIndicator("Classes & Events", null);
    Intent firstIntent= new Intent(this, WebViewActivity.class);
    firstIntent.putExtra("backswipe", false);
    firstSpec.setContent(firstIntent);
    th.addTab(firstSpec);

    TabSpec secondSpec=th.newTabSpec("Twitter");
    secondSpec.setIndicator("Twitter", null);
    Intent secondIntent= new Intent(this, WebViewActivity2.class);
    secondSpec.setContent(secondIntent);
    th.addTab(secondSpec);

    TabSpec thirdSpec=th.newTabSpec("Facebook");
    thirdSpec.setIndicator("Facebook", null);
    Intent thirdIntent= new Intent(this, WebViewActivity3.class);
    thirdSpec.setContent(thirdIntent);
    th.addTab(thirdSpec);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}

}

WebView代码

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

public WebView webView_A;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    webView_A = (WebView) findViewById(R.id.tab1);
    webView_A.getSettings().setJavaScriptEnabled(true);
    webView_A.loadUrl("http://www.eventbrite.com");

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.web_view, menu);
    return true;
}

}

XML主要布局

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFB84D" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-3dip"
        android:layout_weight="0" >
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>

</TabHost>

继承logcat。

  

05-23 01:23:06.886:W / dalvikvm(14673):threadid = 1:线程退出   未捕获的异常(组= 0x4160e930)       05-23 01:23:06.910:E / AndroidRuntime(14673):致命例外:主要       05-23 01:23:06.910:E / AndroidRuntime(14673):java.lang.RuntimeException:无法启动活动   ComponentInfo {com.company.client / com.company.client.MainActivity}:   java.lang.IllegalStateException:你忘了叫'public void   setup(LocalActivityManager activityGroup)'?       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.ActivityThread.access $ 600(ActivityThread.java:153)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.os.Handler.dispatchMessage(Handler.java:99)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.os.Looper.loop(Looper.java:137)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.ActivityThread.main(ActivityThread.java:5226)       05-23 01:23:06.910:E / AndroidRuntime(14673):at java.lang.reflect.Method.invokeNative(Native Method)       05-23 01:23:06.910:E / AndroidRuntime(14673):at java.lang.reflect.Method.invoke(Method.java:511)       05-23 01:23:06.910:E / AndroidRuntime(14673):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)       05-23 01:23:06.910:E / AndroidRuntime(14673):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)       05-23 01:23:06.910:E / AndroidRuntime(14673):at dalvik.system.NativeStart.main(Native Method)       05-23 01:23:06.910:E / AndroidRuntime(14673):引起:java.lang.IllegalStateException:你忘了叫'public void   setup(LocalActivityManager activityGroup)'?       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:747)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.widget.TabHost.setCurrentTab(TabHost.java:413)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.widget.TabHost.addTab(TabHost.java:240)       05-23 01:23:06.910:E / AndroidRuntime(14673):at com.company.client.MainActivity.onCreate(MainActivity.java:28)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.Activity.performCreate(Activity.java:5104)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)       05-23 01:23:06.910:E / AndroidRuntime(14673):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)       05-23 01:23:06.910:E / AndroidRuntime(14673):... 11 more

那么,如何在选项卡中获取我的WebViewActivities,并执行logcat告诉我的操作?

4 个答案:

答案 0 :(得分:0)

Tab-Activity背后的基本概念如下

TabHost 是选项卡式窗口视图的容器。 此对象包含两个子项:用户单击以选择特定选项卡的一组选项卡标签,以及显示该页面内容的FrameLayout对象。

通常使用此容器对象控制单个元素,而不是在子元素本身上设置值。

TabWidget 显示表示父项标签集中每个页面的标签标签列表。此小部件的容器对象是TabHost。 当用户选择一个选项卡时,该对象向容器TabHost发送一条消息,告诉切换显示页面。容器TabHost用于添加标签,添加回调处理程序和管理回调。

所以调整你的布局如下 -

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-3dip"
        android:layout_weight="0" >
    </TabWidget>
  </LinearLayout>

  </TabHost>

和代码段如下 -

    tabSpec = tabHost.newTabSpec("InstructionId");
                           tabSpec.setIndicator("INSTRUCTIONS",getResources().getDrawable(R.drawable.manual));
    intent = new Intent(this, InstructionsScreen.class);
    intent.putExtra("backswipe", false);

    tabSpec.setContent(intent);   // set content must required the first before adding the tab
    tabHost.addTab(tabSpec);

答案 1 :(得分:0)

我注意到的第一个错误是,您在添加Tab后尝试设置内容:

   TabSpec firstSpec=th.newTabSpec("Classes & Events");
   firstSpec.setIndicator("Classes & Events", null);
   Intent firstIntent= new Intent(this, WebViewActivity.class);
   th.addTab(firstSpec);
   firstSpec.setContent(firstIntent);

应该是:

   TabSpec firstSpec=th.newTabSpec("Classes & Events");
   firstSpec.setIndicator("Classes & Events", null);
   Intent firstIntent= new Intent(this, WebViewActivity.class);
   firstSpec.setContent(firstIntent);
   th.addTab(firstSpec);

答案 2 :(得分:0)

我认为你只需要做的就是

TabSpec firstSpec=th.newTabSpec("Classes & Events");
firstSpec.setIndicator("Classes & Events", null);
Intent firstIntent= new Intent(this, WebViewActivity.class);
firstSpec.setContent(firstIntent);
th.addTab(firstSpec);



TabSpec secondSpec=th.newTabSpec("Twitter");
secondSpec.setIndicator("Twitter", null);
Intent secondIntent= new Intent(this, WebViewActivity2.class);
secondSpec.setContent(secondIntent);
th.addTab(secondSpec);



TabSpec thirdSpec=th.newTabSpec("Facebook");
thirdSpec.setIndicator("Facebook", null);
Intent thirdIntent= new Intent(this, WebViewActivity3.class);
thirdSpec.setContent(thirdIntent);
th.addTab(thirdSpec);

因为您需要在tabbar中添加选项卡之前设置内容视图。

希望它能帮到你......

快乐编码:)

答案 3 :(得分:0)

LogCat告诉您已正确初始化tabhost。您应该使用'void setup(LocalActivityManager activityGroup)'。