0

I have two fragment class I am adding with proper fragment transaction following google developer doc. Whenever I am adding a button in my first fragment layout, my app crash. The fragment page does not open in the app. So I tried without the button and everything works properly.

The code is given below: First fragment layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#a6c"
android:padding="16dp"
android:layout_weight="1">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp"
    android:layout_weight="0.7">

    <TextView
        android:text="Word"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:background="#ffffff"
        android:textSize="40dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"/>

    <!--main word load-->
    <TextView android:id="@+id/word"
        style="?android:textAppearanceLarge"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >



       <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/next"
            android:layout_gravity="center"
            android:text="Next"
            android:textSize="?android:textAppearanceLarge"
            />


    </LinearLayout>
    </LinearLayout> </LinearLayout>

This the fragment class

    public static class PlaceholderFragment extends Fragment implements View.OnClickListener {

    public PlaceholderFragment() {
    }


    View v;
    TextView tv1;
    Button checkAnswer,next,prev;

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

        Log.v("Case","Working inside the first");
        tv1 = (TextView) v.findViewById(R.id.word);
        tv1.setText(CardFrontFragment.word[wordC]);
        tv1.setOnClickListener(this);
        next = (Button)v.findViewById(R.id.next);
        next.setOnClickListener(this);

        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next:
                //LOad NExt wOrD
                Log.v("Case","insideCase");
                Fragment fragment = new CardBackFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                fragmentTransaction.replace(R.id.container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
        }
    }}

and the logCat

10-09 15:00:36.320    1850-1850/com.zerothtech.greapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.zerothtech.greapp, PID: 1850
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zerothtech.greapp/com.zerothtech.greapp.CardFrontFragment}: android.view.InflateException: Binary XML file line #43: Error inflating class <unknown>
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
        at android.app.Activity.performStart(Activity.java:5949)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)

        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

The full error log is in > http://pastebin.com/fRjhcqHw            

I am adding them in the frame layout using proper code. I have no problem in adding the fragment. The fragment's functionality stop working when I am adding some buttons (one button does the same as three) in the layout.

Thanks in advance.

3 Answers3

3

logcat:

Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
        at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:572)
        at android.widget.TextView.<init>(TextView.java:999)
        at android.widget.Button.<init>(Button.java:111)

layout:

android:textSize="?android:textAppearanceLarge"

android:textAppearanceLarge does not resolve to a dimension.

Either use android:textApperance attribute in layout, or use a dimension value with textSize.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Logcat points to CardFrontFragment.java:163 that isnt the posted fragment, it is using the same xml? or what? – Nanoc Oct 09 '15 at 10:21
  • You posted an inner class without any outer class. The logcat says there's a problem inflating a `Button` in `CardFrontFragment$PlaceholderFragment` and the `Button` in the XML you posted has a problem with the `textSize` attribute. – laalto Oct 09 '15 at 10:25
0

There in no Button in your xml with id prev. For it, findViewById is returning null, and try to access setOnClickListener trough it causes your app to crash.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Make your Fragment class non-static.I think this will solve your problem.

ch3tanz
  • 3,070
  • 3
  • 16
  • 24
  • Fragment class is always static. It can't be non-static. Besides the fragment class work as long as I don't add anyother button. – MD Kamal Hossain Shajal Oct 09 '15 at 09:12
  • Please post your full xml layout add those buttons as well after which its crashing. – ch3tanz Oct 09 '15 at 09:30
  • 1
    @MDKamalHossainShajal Its crashing due to tv1.setText(CardFrontFragment.word[wordC]); try to set simple text and check. – ch3tanz Oct 09 '15 at 09:37
  • That doesnt make sense, its an InflateException. – Nanoc Oct 09 '15 at 09:45
  • You can check logcat, problem is with your CardFrontFragment and due to InflateExaception its crashing. And he is trying to setText from the word array of CardFrontFragment. – ch3tanz Oct 09 '15 at 09:46
  • Even if CardFrontFragment.word[wordC] was returning null it will not crash. unless theres more logcat trace with an indexoutofbounds exception. – Nanoc Oct 09 '15 at 09:56
  • @Nanoc, there is no index out of bound exception. My array is 1500 in length and it's not empty. It's crashing on the first index – MD Kamal Hossain Shajal Oct 09 '15 at 10:03