代码之家  ›  专栏  ›  技术社区  ›  Matt J.

在sdk 1.5下运行android教程hellotabwidget的错误:“setcurrenttag(getintent())”

  •  3
  • Matt J.  · 技术社区  · 16 年前

    不编译。实际上:即使在1.5中,这个api getintent()也已经被列为不推荐使用的。

    我得到的错误消息抱怨getintent()不返回字符串,但setcurrenttab()需要一个字符串。

    如果我猜并把这行改为:

    “tabhost.setcurrenttab(1);//was setcurrenttab(getintent())”,

    然后它编译、生成,但不运行。我从模拟器收到“stopped unexpectly”错误消息。我甚至无法将log.d输出,所以它似乎很早就“意外”停止了。

    所以第一个也是最主要的问题是:在 http://developer.android.com/resources/tutorials/views/hello-tabwidget.html ?

    第二个更简单的问题是:在发生这种情况的地方,用tabhost替换“mtabhost”是否正确?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Ted    16 年前

    以下是该教程的问题和解决方法:

    步骤2: 创建活动时,如果不通过清单创建它们,则需要手动将它们添加到清单中。

    将这些行添加到androidmanifest.xml:

      <activity android:name=".AlbumsActivity"
                      android:label="@string/app_name"
                      android:theme="@android:style/Theme.NoTitleBar">
            </activity>
      <activity android:name=".ArtistsActivity"
                      android:label="@string/app_name"
                      android:theme="@android:style/Theme.NoTitleBar">
            </activity>
              <activity android:name=".SongsActivity"
                      android:label="@string/app_name"
                      android:theme="@android:style/Theme.NoTitleBar">
            </activity>
    

    步骤3: 只会指示您创建ic_tab_artists.xml文件。您还需要为ic_tab_songs.xml和ic_tab_albums.xml创建一个。您只需复制ic_tab_artists.xml(或更改hellotabview.java选项卡规范,以便对每个选项卡使用artists.xml文件)。

    步骤4: /res/layout/main下倒数第三行有一个输入错误(a;而不是a:)

          android:padding="5dp" />
        </LinearLayout>
    </TabHost>
    

    步骤6: 有一个输入错误使用调用mtabhost而不是tabhost。改变它。

    如前所述,最后一行的getintent()函数不合适。我只是根据它的身份给它打电话。例如:

     tabHost.setCurrentTabByTag("albums");
    
        2
  •  0
  •   MattJ    16 年前

    在非正式的android会议上,我能够确认我的第一个猜测是正确的:在教程中打印的行确实是错误的,应该用类似“tabhost.setcurrenttab(0);//was setcurrenttab(getintent())”的内容替换。

    在运行hellotabwidget教程之前,还有一个重要的遗漏需要修复:albums artists songsActivity都必须添加到manifest.xml清单中。不知何故,教程说明设法忽略了这一要求。