gpt4 book ai didi

java - onCreateView(...) fragment 中的 setText 问题

转载 作者:太空狗 更新时间:2023-10-29 16:41:26 25 4
gpt4 key购买 nike

我知道有人问过类似的问题,但我还是做不到。我有一个 fragment ,在 onCreateView() 中我有一个字符串,我想将其设置为等于我的 textView 中的文本。但是我不能让它工作。有人可以给我一个如何编写它的代码示例吗?

例如,我曾尝试过这个导致应用程序崩溃的方法:

TextView text = (TextView) getActivity().findViewById(R.id.sektion_name_detailed_view);
text.setText("test");

堆栈跟踪:

06-12 23:02:59.926: W/dalvikvm(2402): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-12 23:02:59.933: E/AndroidRuntime(2402): FATAL EXCEPTION: main
06-12 23:02:59.933: E/AndroidRuntime(2402): java.lang.NullPointerException
06-12 23:02:59.933: E/AndroidRuntime(2402): at se.Appsolut.student.DetailedViewSektionerFragment.onCreateView(DetailedViewSektionerFragment.java:21)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.os.Handler.handleCallback(Handler.java:615)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.os.Looper.loop(Looper.java:137)
06-12 23:02:59.933: E/AndroidRuntime(2402): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-12 23:02:59.933: E/AndroidRuntime(2402): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 23:02:59.933: E/AndroidRuntime(2402): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 23:02:59.933: E/AndroidRuntime(2402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-12 23:02:59.933: E/AndroidRuntime(2402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-12 23:02:59.933: E/AndroidRuntime(2402): at dalvik.system.NativeStart.main(Native Method)

最佳答案

如果它是一个 fragment ,那么在访问元素之前,您必须先在 onCreateView 上膨胀布局。

LayoutInflater lf = getActivity().getLayoutInflater(); 
View view = lf.inflate(R.layout.fragment, container, false); //pass the correct layout name for the fragment

TextView text = (TextView) view.findViewById(R.id.sektion_name_detailed_view);
text.setText("Just another sample");

这应该可以解决您的问题。

关于java - onCreateView(...) fragment 中的 setText 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com