尝试动态生成选项卡及其视图

时间:2013-01-10 04:49:31

标签: android dynamic tabs

更新

编译错误已经解决,我根据我正在关注的示例找到了一些我没有正确执行的操作并更正了它们。但是,当我尝试运行代码时,我收到以下错误。当我逐行浏览代码时,我注意到NewT​​ab.setContent中的createTabContent没有执行,因为我的断点从未跳过。

01-12 10:11:21.285: E/AndroidRuntime(14528): FATAL EXCEPTION: main
01-12 10:11:21.285: E/AndroidRuntime(14528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jackman.universalav/com.jackman.universalav.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.os.Looper.loop(Looper.java:137)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-12 10:11:21.285: E/AndroidRuntime(14528): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 10:11:21.285: E/AndroidRuntime(14528): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 10:11:21.285: E/AndroidRuntime(14528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-12 10:11:21.285: E/AndroidRuntime(14528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-12 10:11:21.285: E/AndroidRuntime(14528): at dalvik.system.NativeStart.main(Native Method)
01-12 10:11:21.285: E/AndroidRuntime(14528): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.content.res.Resources.getValue(Resources.java:1026)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2131)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.content.res.Resources.getLayout(Resources.java:865)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:554)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.widget.TabHost.addTab(TabHost.java:234)
01-12 10:11:21.285: E/AndroidRuntime(14528): at com.jackman.universalav.MainActivity.onCreate(MainActivity.java:71)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.Activity.performCreate(Activity.java:5191)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-12 10:11:21.285: E/AndroidRuntime(14528): ... 11 more

更新的代码是:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Create the tab host that contains the tab widget that contains the tabs
        TabHost Tabs = new TabHost(this);
        Tabs.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //Create the tab widget  container
        TabWidget TabWidget = new TabWidget(this);
        TabWidget.setId(android.R.id.tabs);
        Tabs.addView(TabWidget, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //the tab host needs a frame layout for the views associated with each visible tab
        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setId(android.R.id.tabcontent);
        frameLayout.setPadding(0, 65, 0, 0);
        Tabs.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        Tabs.setup();
        //Create the tabs
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " + dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
            " = d." + dbHelper.Attribute_Device_ID + ";";
        Cursor c = db.rawQuery(dbQuery, null);
        if (c.getCount() > 0)
        {
            while (c.moveToNext())
            {
                  int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
                  int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
                  final String DeviceType = c.getString(iColumnDeviceType);
                  String DeviceName = c.getString(iColumnDeviceName);
                  String DeviceNameLabel = DeviceName.replaceAll(" ", "");
                  TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
                  NewTab.setIndicator(DeviceNameLabel);
                  NewTab.setContent(new TabHost.TabContentFactory()
                  {
                    public View createTabContent(String tag)
                    {
                        LinearLayout ll = new LinearLayout(MainActivity.this);
                        LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                        ll.setLayoutParams(params);
                        ll.setOrientation(LinearLayout.VERTICAL);
                        if (DeviceType == "Receiver")
                        {

                        }
                        else if (DeviceType == "Video Player")
                        {

                        }
                        return ll;
                     }
                   });
                   Tabs.addTab(NewTab);
             }
        }
    }
}

结束更新

下面的原始问题:

我还没有完成这个例行程序。

我试图关注此链接中的代码示例。 http://www.pocketmagic.net/2010/01/android-dynamic-tab-control/

然而,当我尝试为正在创建的选项卡创建一个linearlayout时,编译器就吓坏了我。我不确定为什么会抛出这个错误。这就像我之前制作的一个对象不存在。

返回的错误是:

Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token "params", VariableDeclaratorId expected after this token

无论如何代码如下。谁能看到我做错了什么?

//Create the tab host that contains the tab widget that contains the tabs
TabHost Tabs = new TabHost(this);
Tabs.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//Create the tab widget  container
TabWidget TabWidget = new TabWidget(this);
TabWidget.setId(android.R.id.tabs);
Tabs.addView(TabWidget, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Tabs.setup();
//Create the tabs
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " +
    dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
    " = d." + dbHelper.Attribute_Device_ID + ";";
Cursor c = db.rawQuery(dbQuery, null);
if (c.getCount() > 0)
{
    while (c.moveToNext())
    {
        int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        final String DeviceType = c.getString(iColumnDeviceType);
        String DeviceName = c.getString(iColumnDeviceName);
        String DeviceNameLabel = DeviceName.replaceAll(" ", "");
        TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
        NewTab.setIndicator(DeviceName);
        NewTab.setContent(new TabHost.TabContentFactory()
        {
            LinearLayout ll = new LinearLayout(MainActivity.this);
            LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            ll.setLayoutParams(params); //this line has both of the errors
            public View createTabContent(String tag)
            {
                    if (DeviceType == "Receiver")
                    {

                    }
                    else if (DeviceType == "Video Player")
                    {

                    }
                    return null; //place holder until I finish the routine
                }
            });
            Tabs.addTab(NewTab);
        }
    }

1 个答案:

答案 0 :(得分:0)

经过多次试验和错误后我想出了这个问题,然后在Google上搜索了大量的例子。

首先,我放弃尝试通过java而不是xml为活动创建选项卡小部件和框架布局。

在我的活动布局xml文件中,我添加了一个选项卡控件,并在xml视图中删除了选项卡,使xml代码看起来像这样。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

        <LinearLayout
        android:id="@+id/MainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

然后在活动的java文件中,我更新了oncreate例程。

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Create the tabs
TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " +
    dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
    " = d." + dbHelper.Attribute_Device_ID + ";";
Cursor c = db.rawQuery(dbQuery, null);
int Count = 0;
if (c.getCount() > 0)
{
    while (c.moveToNext())
    {
        Count = Count + 1;
        int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        final String DeviceType = c.getString(iColumnDeviceType);
        final String DeviceName = c.getString(iColumnDeviceName);
        final String DeviceNameLabel = DeviceName.replaceAll(" ", "");
        Log.d("Creating Tab", "TabSpec: tab" + DeviceNameLabel);
        Log.d("Creating Tab", "Indicator: " + DeviceName);
        final TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
        NewTab.setIndicator(DeviceName);
        NewTab.setContent(new TabHost.TabContentFactory()
        {
            @Override
            public View createTabContent(String tag)
            {
                LinearLayout ll = new LinearLayout(MainActivity.this);
                LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                ll.setLayoutParams(params);
                ll.setOrientation(LinearLayout.VERTICAL);
                if (DeviceType.equals("Receiver"))
                {
                    final Button btnVolumeUp = new Button(MainActivity.this);
                    btnVolumeUp.setText("Volume Up");
                    btnVolumeUp.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnVolumeUp.setGravity(Gravity.LEFT);
                    ll.addView(btnVolumeUp);
                }
                else if (DeviceType.equals("Video Player"))
                {
                    final Button btnPlay = new Button(MainActivity.this);
                    btnPlay.setText("Play");
                    btnPlay.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnPlay.setGravity(Gravity.LEFT);
                    ll.addView(btnPlay);
                }
                return ll;
            }
        });

        Tabs.addTab(NewTab);

    }
}
相关问题