我有一个简单的 Android 应用程序,其中包含使用 Tabhost 和 Fragments 设置的选项卡。在其中一个标签页上, fragment 是一个 ListView 。我想要做的是,当用户点击 ListView 的其中一项时,根据点击的项目打开一个包含更具体信息的新 View ,但保留它以便选项卡仍然存在。
我的方法是,当用户单击 ListView 中的项目时,将创建一个新 fragment ,然后从拥有所有这些 fragment 并处理标签逻辑的我的主 FragmentActivity 类中,它将分离当前显示的 fragment 并添加这个"new"(singleStationListItem) fragment 。我遇到的问题是我无法使用 R.id.realtabcontent 将我的新 fragment 添加到 fragmentTransaction 中,因此,它没有正确添加。我得到:
03-21 10:50:01.862: E/FragmentManager(1136): 没有找到 fragment singleStationListItem 的 id 0x1010000 (android:attr/theme) 的 View {40d090a0 #2 id=0x1010000}
其中 R.id.realtabcontent = 0x01010000;
我可以在没有 Id 的情况下附加它,但是用户无法返回(调用 addtobackstash() )。我已经搜索了这个错误,但其他解决方案与 tabhost 无关,我似乎做了所需的事情,比如在 onCreate() 中调用 setContextView(...) 到布局的 xml 文件,其中包含我的 framelayout 标记试图将我的 fragment 附加到。我可以毫无问题地将我的选项卡 fragment 添加到 R.id.realtabcontent。有谁知道是什么导致了我的错误以及我该如何解决?
注意:我按照本教程使我的选项卡正常工作。我的代码非常相似。 http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
我的代码: activiy_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
android:padding="5dp" />
<FrameLayout
android:id="@+android:id/realtabcontent" <--id that I want to set to
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0"
android:orientation="horizontal" />
</LinearLayout>
</TabHost>
</LinearLayout>
main_activity.java//我的 fragment Activity 类的部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Step 1: Inflate layout
setContentView(R.layout.activity_main);
// Step 2: Setup TabHost
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
}
public void onTabChanged(String tag) { //handles tab changes
TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); //note it uses the R.id.realtabcontent without any issues here
} else {
ft.attach(newTab.fragment);
}
}
Log.d("fragment manager in activity: ", "" + ft.isEmpty());
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
Log.d("ft ID: ", ft.toString());
}
}
//This is the callback function inside the listview fragment. I created an interface and it is overwritten in here as suggested by the Android SDK guide
public void onStationSelected(String station) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.addToBackStack(null);
//create argument for new fragment that will be created
Bundle b = new Bundle();
b.putString("station", station);
Fragment displayStationInfo = Fragment.instantiate(this, singleStationListItem.class.getName(), b);
Fragment cur = getSupportFragmentManager().findFragmentById(R.id.realtabcontent);
ft.detach(cur);
ft.add(R.id.realtabcontent, displayStationInfo); <-- this line causes the error
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
singleStationListItem.java 在这里
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = this.getArguments();
getActivity().setContentView(R.layout.single_station_item_view);
TextView txtStation = (TextView) getActivity().findViewById(R.id.station_label);
String station = b.getString("station");
// displaying selected product name
Log.d(TAG, "passed in station information: " + station);
txtStation.setText(station);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = (LinearLayout)inflater.inflate(R.layout.single_station_item_view, container, false);
return v;
}
如有任何帮助,我们将不胜感激。
最佳答案
我知道不久前有人问过这个问题,我正在为可能遇到这个问题的其他人发布答案。
据我所知,没有'realtabcontent'的Android id。在您的 XML 中,删除 FrameLayout 的 id 的“android”部分。
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
developer documentation 上发布的完整 XML 代码如下:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
祝你好运,编码愉快!
关于Android fragment ,在 TabHost 中找不到 id 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15545584/
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10
除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p
我花了几天时间尝试安装ruby1.9.2并让它与gems一起工作:-/我最终放弃了我的MacOSX10.6机器,下面是我的Ubuntu机器上的当前状态。任何建议将不胜感激!#rubytest.rb:29:in`require':nosuchfiletoload--mongo(LoadError)from:29:in`require'fromtest.rb:1:in`'#cattest.rbrequire'mongo'db=Mongo::Connection.new.db("mydb")#gemwhichmongo/usr/local/rvm/gems/ruby-1.9.2-p0/g
因此,当我遵循MichaelHartl的RubyonRails教程时,我注意到在用户表中,我们为:email属性添加了一个唯一索引,以提高find的效率方法,因此它不会逐行搜索。到目前为止,我们一直在根据情况使用find_by_email和find_by_id进行搜索。然而,我们从未为:id属性设置索引。:id是否自动索引,因为它在默认情况下是唯一的并且本质上是顺序的?或者情况并非如此,我应该为:id搜索添加索引吗? 最佳答案 大多数数据库(包括sqlite,这是RoR中的默认数据库)会自动索引主键,对于RailsMigration