已经搜索过这个,但他们每个人都是 onCreateView() 方法,您可以通过充气器访问 Fragments 视图。
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
super.onCreateView(inflater, group, saved);
View view = inflater.inflate(R.layout.photosfrag, group, false);
我想知道的是如何动态地做到这一点。例如说我正在使用一个活动来显示一个片段并在片段中调用一个方法,例如:
public void setTitleText(String title) {
TextView nameView = (TextView)getView().findViewById(R.id.titleTxtView);
nameView.setText(title);
}
仅仅因为片段已经被创建并且它是一种动态改变它的方式。
任何帮助都会很重要。我可能做错了。
编辑
这是崩溃日志
06-25 17:31:37.343: D/AndroidRuntime(1009): Shutting down VM
06-25 17:31:37.353: W/dalvikvm(1009): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-25 17:31:37.383: E/AndroidRuntime(1009): FATAL EXCEPTION: main
06-25 17:31:37.383: E/AndroidRuntime(1009): java.lang.NullPointerException
06-25 17:31:37.383: E/AndroidRuntime(1009): at com.corecoders.stuart.MainActivity.onTrackSelected(MainActivity.java:81)
06-25 17:31:37.383: E/AndroidRuntime(1009): at com.corecoders.stuart.HistoryFragment.onListItemClick(HistoryFragment.java:51)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AbsListView$1.run(AbsListView.java:3168)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.os.Handler.handleCallback(Handler.java:605)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.os.Looper.loop(Looper.java:137)
06-25 17:31:37.383: E/AndroidRuntime(1009): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-25 17:31:37.383: E/AndroidRuntime(1009): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 17:31:37.383: E/AndroidRuntime(1009): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 17:31:37.383: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 17:31:37.383: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 17:31:37.383: E/AndroidRuntime(1009): at dalvik.system.NativeStart.main(Native Method)