0

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.

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Now, I am using the provided inflater. Everything is in place. Still getting the same error.
0

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.

2 Comments

I inflated correct layout. Also I am having the textview with same id. I have cross checked. Thanks
@Nemat that cannot be. You are missing something. NPE is because text is null and its not initialized properly. Again i stress the point that you infalte wrong layout or you referenced wrong id.
0

check that your ride_details_fragment contains textView with id txt_ride_name or not. this would be cause of this exception.

1 Comment

thanks.... But I dont know what went wrong. I created another layout, copy-pasted the last one in this and it works fine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.