Reputation: 29
I am working on fragments in my current application.
I am displaying 3 different tabs each of which having a fragment. I am getting a null pointer exception while accessing the view. Here is the code snippet:
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);
}
//
}
Exception is in line text.setText. Here is the exception logs:
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
Can somebody help me in finding the root cause of the exception? It would be a great to help me.
Thanks.
Upvotes: 0
Views: 126
Reputation: 1536
check that your ride_details_fragment
contains textView with id txt_ride_name
or not. this would be cause of this exception.
Upvotes: 0
Reputation: 11234
Why are you trying to get another inflater instead of provided one?
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;
}
And please make sure you have id = txt_ride_name
(case-sensitive) inside ride_details_fragment.xml
Upvotes: 1
Reputation: 133560
Exception is in line text.setText.
Your ride_details_fragment.xml
does not have a TextView
with id txt_ride_name
.
Either you inflated the wrong layout or your layout does not have the view with the id specified.
Upvotes: 0