我知道人们问过类似的问题,但我仍然无法解决。我有一个片段,并且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)