如果选项卡和活动不在一起......有没有办法作弊?

发布于 2024-09-12 09:05:59 字数 2076 浏览 3 评论 0原文

我很难将我的活动放入选项卡中。我有一个活动可以解析 XML 文件并将它们放入列表中,并且它本身可以完美运行。当我在选项卡上调用它时,它不起作用(我收到可怕的“抱歉!等等……意外停止”提示...顺便说一句,是的,我做了清单)。

我已将这些活动迁移为一项活动,瞧!成功了!!!然而,这不是我们想要的这个项目的方式——我们真的需要有单独的活动。

许多人发现选项卡和活动不能很好地协同工作,有没有办法解决这个问题?也许某种乳化剂?

这是代码:

导入android.app.Activity; 导入 android.content.Intent; 导入 android.os.Bundle; 导入 android.widget.TabHost; 导入 android.widget.Toast;

public class TabDemo extends Activity{

/** data members go here*/

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    try {
        TabHost tabs= (TabHost)findViewById(R.id.tabhost);

        tabs.setup();
        Intent callResultHits = new Intent(this, my.tabebd.layout.ResultHits.class);

        TabHost.TabSpec spec = tabs.newTabSpec("tag1");
        spec.setContent(callResultHits);
        spec.setIndicator("Result",   getResources().getDrawable(R.drawable.ic_tab_search_result) );
        tabs.addTab(spec);

        spec = tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Details",getResources().
                             getDrawable(R.drawable.ic_tab_details));
        tabs.addTab(spec);

        tabs.setCurrentTabByTag("tag1");
    } catch (Throwable t) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "Exception: " + t.toString(), 50000).show();
    }
}

这里是活动之一...

public class ResultHits extends Activity implements OnItemClickListener {
ListView listView_titles;
ArrayList<String> items = new ArrayList<String>();
String [] test = {"1", "2","3"};

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



    listView_titles = (ListView)findViewById(R.id.list);

    listView_titles.setAdapter(new ArrayAdapter<String>
    (this,R.layout.row, R.id.row_text,test));


}

}

我省略了 xml 解析部分...如果这个基本列表可以显示在选项卡中那么它就完美了。提前TY

BTW setcurrenttabByTag 之前是setCurrenttab(2)..实际上我做了这些值0,1,2,3以防万一;)

I am having a real hard time on putting my activities in tabs. I have an activity that parses an XML file and puts them on a list, and it works perfectly on its own. When I call it on a tab however it does not work (i get the dreaded "Sorry! blah blah.. has stopped unexpectedly" prompt... BTW yes I did the manifest).

I have migrated the activities to work as one activity, voila! it worked!!! However this is not the way we wanted to go with this project - WE REALLY NEED to have separate activities.

So as many people had found out that tabs and activities doesn't work well together is there a way to work around it? Some sort of emulsifier maybe?

Here is the code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;

public class TabDemo extends Activity{

/** data members go here*/

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    try {
        TabHost tabs= (TabHost)findViewById(R.id.tabhost);

        tabs.setup();
        Intent callResultHits = new Intent(this, my.tabebd.layout.ResultHits.class);

        TabHost.TabSpec spec = tabs.newTabSpec("tag1");
        spec.setContent(callResultHits);
        spec.setIndicator("Result",   getResources().getDrawable(R.drawable.ic_tab_search_result) );
        tabs.addTab(spec);

        spec = tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Details",getResources().
                             getDrawable(R.drawable.ic_tab_details));
        tabs.addTab(spec);

        tabs.setCurrentTabByTag("tag1");
    } catch (Throwable t) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "Exception: " + t.toString(), 50000).show();
    }
}

here is one of the activities...

public class ResultHits extends Activity implements OnItemClickListener {
ListView listView_titles;
ArrayList<String> items = new ArrayList<String>();
String [] test = {"1", "2","3"};

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



    listView_titles = (ListView)findViewById(R.id.list);

    listView_titles.setAdapter(new ArrayAdapter<String>
    (this,R.layout.row, R.id.row_text,test));


}

}

I omitted the xml parsing part... if this basic list can be shown inside the tab then it will be perfect. TY in advance

BTW setcurrenttabByTag was previously setCurrenttab(2)..actualy i did these values 0,1, 2, 3 just in case ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-09-19 09:05:59

发布您的代码我认为您将活动放入每个选项卡中不会有问题,
这是如何将活动放入选项卡的基本示例,

    static  TabHost tabHost;

...
...
...

            tabHost = getTabHost();  // The activity TabHost
           TabHost.TabSpec spec;  // Resusable TabSpec for each tab
           Intent intent;  // Reusable Intent for each tab
           // Create an Intent to launch an Activity for the tab (to be reused)
           intent = new Intent().setClass(this, MainActivity.class);
           // Initialize a TabSpec for each tab and add it to the TabHost
           spec = tabHost.newTabSpec("MainActivity")     
             .setIndicator(null, null)
               .setContent(intent);
           tabHost.addTab(spec);

           // Do the same for the other tabs
           intent = new Intent().setClass(this, SecondActivity.class);
           spec = tabHost.newTabSpec("SecondActivity")         
           .setIndicator(null, null)               
           tabHost.addTab(spec);

           intent = new Intent().setClass(this, ThirdActivity.class);
           spec = tabHost.newTabSpec("ThirdActivity")
               .setIndicator(null, null)
               .setContent(intent);
           tabHost.addTab(spec);

           intent = new Intent().setClass(this, FourthActivity.class);
           spec = tabHost.newTabSpec("FourthActivity")
               .setIndicator(null, null)
               .setContent(intent);
           tabHost.addTab(spec);

Post your code I think you will not have problems putting activities into every tab,
This is a basic Example of how put activities in tabs,

    static  TabHost tabHost;

...
...
...

            tabHost = getTabHost();  // The activity TabHost
           TabHost.TabSpec spec;  // Resusable TabSpec for each tab
           Intent intent;  // Reusable Intent for each tab
           // Create an Intent to launch an Activity for the tab (to be reused)
           intent = new Intent().setClass(this, MainActivity.class);
           // Initialize a TabSpec for each tab and add it to the TabHost
           spec = tabHost.newTabSpec("MainActivity")     
             .setIndicator(null, null)
               .setContent(intent);
           tabHost.addTab(spec);

           // Do the same for the other tabs
           intent = new Intent().setClass(this, SecondActivity.class);
           spec = tabHost.newTabSpec("SecondActivity")         
           .setIndicator(null, null)               
           tabHost.addTab(spec);

           intent = new Intent().setClass(this, ThirdActivity.class);
           spec = tabHost.newTabSpec("ThirdActivity")
               .setIndicator(null, null)
               .setContent(intent);
           tabHost.addTab(spec);

           intent = new Intent().setClass(this, FourthActivity.class);
           spec = tabHost.newTabSpec("FourthActivity")
               .setIndicator(null, null)
               .setContent(intent);
           tabHost.addTab(spec);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文