我正在处理当前应用程序中的片段。
我正在显示3个不同的标签,每个标签都有一个片段。我在访问视图时收到空指针异常。以下是代码段:
public class DetailsFragment extends Fragment {
private TextView text;
public DetailsFragment() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.ride_details_fragment,
null);
text = (TextView) v.findViewById(R.id.txt_ride_name);
text.setText("Current Tab is: ");
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
//
}
异常与text.setText一致。以下是例外日志:
03-24 19:15:40.138: E/AndroidRuntime(13611): FATAL EXCEPTION: main
03-24 19:15:40.138: E/AndroidRuntime(13611): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bb/com.android.ui.HomeActivity}: java.lang.NullPointerException
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.ActivityThread.access$700(ActivityThread.java:134)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.os.Looper.loop(Looper.java:137)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.ActivityThread.main(ActivityThread.java:4856)
03-24 19:15:40.138: E/AndroidRuntime(13611): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 19:15:40.138: E/AndroidRuntime(13611): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 19:15:40.138: E/AndroidRuntime(13611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-24 19:15:40.138: E/AndroidRuntime(13611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-24 19:15:40.138: E/AndroidRuntime(13611): at dalvik.system.NativeStart.main(Native Method)
03-24 19:15:40.138: E/AndroidRuntime(13611): Caused by: java.lang.NullPointerException
03-24 19:15:40.138: E/AndroidRuntime(13611): at com.android.ride.ui.DetailsFragment.onCreateView(DetailsFragment.java:43)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.Activity.performStart(Activity.java:5057)
03-24 19:15:40.138: E/AndroidRuntime(13611): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2054)
03-24 19:15:40.138: E/AndroidRuntime(13611): ... 11 more
有人可以帮助我找到异常的根本原因吗?帮助我会很棒。
感谢。
答案 0 :(得分:1)
你为什么试图获得另一个inflater而不是提供?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.ride_details_fragment,
container, false);
text = (TextView) v.findViewById(R.id.txt_ride_name);
text.setText("Current Tab is: ");
return v;
}
请确保txt_ride_name
ride_details_fragment.xml
(区分大小写)
答案 1 :(得分:0)
异常是在text.setText。
中
您的ride_details_fragment.xml
没有身份TextView
的{{1}}。
要么夸大错误的布局,要么你的布局没有指定id的视图。
答案 2 :(得分:0)
检查您的ride_details_fragment
是否包含ID为txt_ride_name
的textView。这可能是造成这种例外的原因。