【发布时间】:2014-06-12 15:18:08
【问题描述】:
我有一个支持的片段活动,它将加载差异片段。该片段有一些textView 和id = "score",我想获得它的句柄,但findViewById 的分数textView 返回null。为什么会这样?
textView放在fragment中
public class MyActivity extends extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks{
private TextView scoreBoardTextView = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
scoreBoardTextView = (TextView) findViewById(R.id.score); //this returns null
}
@Override
public void onNavigationDrawerItemSelected(int position) {
//set fragment
}
}
【问题讨论】:
-
你的textview放在哪里?它在片段布局中吗?如果是,那么您必须调用 scoreBoardTextView = (TextView) findViewById(R.id.score);在片段的 onCreateView 中。像这样的东西:stackoverflow.com/questions/24156139/…
-
在片段中。在片段中
-
您无法从单独的 lYout 获取视图。
-
@Rod_Algonquin 没有找到你
-
你想从Activity更新Fragment的TextView吗?您最好在 Fragment 本身中执行此操作。