Android Fragment - Inflate/Add view after onCreateView

994 views Asked by At

I'm trying to inflate and add a view(XML layout file) within a method after onCreate. The parent is a LinearLayout placed in a FRAGMENT layout, which I access through findViewById with the Fragment view. However I can't get the view to inflate/added(it has to be added dynamically since the user adds these views).

Snippet of my onCreate:

CallFragment callFragment = (CallFragment) tabsPagerAdapter.getItem(1);
callRootView = callFragment.getView();

mainLayout = (LinearLayout)callRootView.findViewById(R.id.call_main_layout);

Snippet of my CreateCall method:

private void CreateCall()
{
    //try{
        LayoutInflater i = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout c = (LinearLayout)i.inflate(R.layout.call_display, null);
        //c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 50));
        mainLayout.addView(c);
        Log.i("Success", "");
    /*}catch (Exception e) {
        Log.i("ERROR", e.toString());
    }*/
}

My LogCat:

06-26 09:48:08.644  25001-25001/com.voytech.stockcall E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.voytech.stockcall, PID: 25001
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.voytech.stockcall/com.voytech.stockcall.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.voytech.stockcall.MainActivity.onCreate(MainActivity.java:148)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

MY SECOND APPROACH: This code is all inside my fragment, still doesn't work. The method is accessed from my MainActivity, hopefully that's not an issue. onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.call_fragment, container, false);

    mainLayout = (LinearLayout) rootView.findViewById(R.id.call_main_layout);      //mainLayout references our direct LinearLayout(the container) in our XML document, this is also the parent that we will add the child view to

//Reference inflater and container if needed
layoutInflater = inflater;
        layoutContainer = container;
    }

This is the AddCallLayout method which is called from our MainActivity through a fragment TabsPagerAdapter, I removed some commented out code since this is only one of the multiple approaches I've tried:

public void AddCallLayout()
{
    callDisplay = layoutInflater.inflate(R.layout.call_display, layoutContainer, false);
    mainLayout.addView(callDisplay);
}

Fragment LogCat:

06-26 10:38:13.840  14354-14354/com.voytech.stockcall E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.voytech.stockcall, PID: 14354
java.lang.NullPointerException
        at com.voytech.stockcall.CallFragment.AddCallLayout(CallFragment.java:146)
        at com.voytech.stockcall.MainActivity$2.onClick(MainActivity.java:675)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
0

There are 0 answers