Mark Dylan B Mercado Mark Dylan B Mercado - 26 days ago 7
Android Question

Create your own design inside Fragment programmatically error java.lang.IllegalArgumentException

Good day guys,
I'm just new at android development, I'm creating a design inside the fragment programmatically without the use of xml,
I'm trying to add textview and imageview inside my Fragment Programmatically using java

here is my Tab1 class that extends Fragment

public class Tab1 extends Fragment {

LinearLayout linearLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);

linearLayout = (LinearLayout)rootView.findViewById(R.id.linear);
getActivity().setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);

TextView textView = new TextView(getActivity());
textView.setText("example");
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);

ImageView imageView = new ImageView(getActivity());
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setImageResource(R.drawable.placeholder_icon);
linearLayout.addView(imageView);

TextView textView2 = new TextView(getActivity());
textView.setText("example2");
linearLayout.addView(textView2);

return rootView;
}


}

And here is my xml,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.gawapa.exampleproject.MainActivity">




But it gives me this error

FATAL EXCEPTION: main
Process: com.gawapa.exampleproject, PID: 15104
java.lang.IllegalArgumentException: No view found for id 0x7f0c0071 (com.gawapa.exampleproject:id/container) for fragment Tab2{41eb5380 #1 id=0x7f0c0071 android:switcher:2131492977:1}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:765)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16677)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
at android.view.View.measure(View.java:16677)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1927)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1119)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5652)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
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:1268)
at com.android.internal.os.ZygoteInit.main(Zygote


How can I fix this?
I'm trying to add textview and imageview inside my Fragment Programmatically using java, but the stacktrace doesn't give me my lines of code that is wrong

Answer Source

Try something like this... And remove getActivity().setContentView(linearLayout);. Belongs in the activity.

public class Tab1 extends Fragment {

LinearLayout linearLayout;
View rootView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   rootView = inflater.inflate(R.layout.tab1, container, false);

   return rootView;
}

@Override 
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    linearLayout = (LinearLayout)rootView.findViewById(R.id.linear);

    linearLayout.setOrientation(LinearLayout.VERTICAL);

    TextView textView = new TextView(getActivity());
    textView.setText("example");
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    linearLayout.addView(textView);

    ImageView imageView = new ImageView(getActivity());
    imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    imageView.setImageResource(R.drawable.placeholder_icon);
    linearLayout.addView(imageView);

    TextView textView2 = new TextView(getActivity());
    textView.setText("example2");
    linearLayout.addView(textView2);
}
}