我知道人们问了类似的问题,但我仍然无法管理。我有一个片段,在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)
答案 0 :(得分:4)
如果是片段,那么在访问元素之前,您必须先在onCerateView
上对布局进行充气。
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("test");
这可以解决您的问题。
答案 1 :(得分:0)
我想也许你编辑像
TextView text = (TextView) findViewById(R.id.sektion_name_detailed_view);
text.setText("test");
答案 2 :(得分:0)
我的方法与在片段中设置文本有些不同。基本上,我使用view Object在Fragment中设置Text。因为onCreateView()使用Inflater类的inflate方法返回每个组件的视图。因此,我已经全局创建了一个View对象,并将其分配给inflate方法,并在onCtreateView()方法中将其返回。因此,结果TextView组件已经在视图内部,因此我可以在任何需要的地方使用它。
View inflatedview = null;
public CustomerOutstanding() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflatedview = inflater.inflate(R.layout.fragment_customer_outstanding, container, false);
TotalAmount = (TextView)inflatedview.findViewById(R.id.TotalAmountOutstanding);
return inflatedview;
}