1

I am trying to dynamically add rows to a table in the fragment. However, I am facing some run-time error and seriously in need of some advice.

Logcat:

1)E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lol/com.example.lol.MainActivity}: android.view.InflateException: Binary XML file line #96: Error inflating class fragment

2)E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

3)E/AndroidRuntime(1038):at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2084)

FragmentTwo.Java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FragmentTwo extends Fragment {

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

        TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);


        TableRow tr = new TableRow(getActivity().getApplicationContext());
//        tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


        TextView textview1 = new TextView(getActivity().getApplicationContext());
        textview1.setText("unhappy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity().getApplicationContext());
        textview2.setText("happy");
        tr.addView(textview2);


        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        return view;
    }

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view) {
        Fragment fr;    
        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }else {
            fr = new FragmentOne();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();

   }

}

fragment_two.xml

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:background="#ffff00">


        <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainLayout">

        <TableRow
                android:id="@+id/infoRow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                        android:text="Unhappy"
                        android:id="@+id/column1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                </TextView>

                <TextView
                        android:text="Happy"
                        android:id="@+id/column2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                </TextView>


        </TableRow>
</TableLayout>
</LinearLayout>
lonelearner
  • 1,637
  • 4
  • 14
  • 22

2 Answers2

1

Change this

TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);

to

TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout);

This is one mistake which avoids NullPointerException.

And use getActivity() to initialize views. To know why read

When to call activity context OR application context?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks for the guidance. I understand why we use getActivity() instead of getApplicationContext() which is last longer than the activity. – lonelearner Apr 02 '14 at 09:55
  • Your advices works but now i encounter another problem. Previously there are 2 buttons, onclicking each button will link the user straight to the respective fragment. When i implement this solution, the button for the fragment 1 does not work. Should i post this question on a another thread? Kindly seek your advice – lonelearner Apr 02 '14 at 09:57
  • @lonelearner it will work if you do it right. Yes new peoblem post a new question and editing this question will change completely – Raghunandan Apr 02 '14 at 09:58
  • for those learning more about fragments, refer previously to my post http://stackoverflow.com/questions/22668049/getting-input-from-edittext-set-text-in-textview-of-a-fragment/22668517?noredirect=1#comment34571718_22668517 – lonelearner Apr 02 '14 at 10:01
  • Dear Sir, i have posted my new problem in the following url. http://stackoverflow.com/questions/22833296/fragment-intermediateiii-creating-a-activity-that-alternate-between-fragments/22833413#22833413 – lonelearner Apr 03 '14 at 09:33
1

I had the same issue, and it was because I passed the application context instead of the activity context.

        TableRow tr = new TableRow(getActivity());

        TextView textview1 = new TextView(getActivity());
        textview1.setText("unhappy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("happy");
        tr.addView(textview2);

also you should read trought this

Android: How to track down the origin of a InflateException?

Community
  • 1
  • 1
Matej Špilár
  • 2,617
  • 3
  • 15
  • 28