0

I've recently studyed android by myself. and I tried Using Fragment by following my Textbook but I met an error even though I typed codes exactly the same as my textbook.

Can you guys explain to me what is wrong?

This is MainActivity.java

package com.example.ex_1;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public static class CounterFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

            View root = inflater.inflate(R.layout.counterfragment, container, false);
            Button btnIncrease = (Button)root.findViewById(R.id.btnincrease);
            final TextView textCounter = (TextView)root.findViewById(R.id.txtcounter);
            btnIncrease.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int count = Integer.parseInt(textCounter.getText().toString());
                    textCounter.setText(Integer.toString(count + 1));
                }

            });
            return root;
        }

    }
}

This is activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="~~"/>
    <fragment 
        android:name="com.example.ex_1.MainActivity$CounterFragment"
        android:id="@+id/counterfragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

And the error is

09-24 07:40:08.329: E/AndroidRuntime(1794): FATAL EXCEPTION: main
09-24 07:40:08.329: E/AndroidRuntime(1794): Process: com.example.ex_1, PID: 1794
09-24 07:40:08.329: E/AndroidRuntime(1794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex_1/com.example.ex_1.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.os.Looper.loop(Looper.java:135)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.ActivityThread.main(ActivityThread.java:5221)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Native Method)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Method.java:372)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
09-24 07:40:08.329: E/AndroidRuntime(1794): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.Activity.setContentView(Activity.java:2144)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at com.example.ex_1.MainActivity.onCreate(MainActivity.java:19)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.Activity.performCreate(Activity.java:5937)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
09-24 07:40:08.329: E/AndroidRuntime(1794):     ... 10 more
09-24 07:40:08.329: E/AndroidRuntime(1794): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.ex_1.MainActivity$CounterFragment that is not a Fragment
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.Fragment.instantiate(Fragment.java:606)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.Fragment.instantiate(Fragment.java:582)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.app.Activity.onCreateView(Activity.java:5286)
09-24 07:40:08.329: E/AndroidRuntime(1794):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
09-24 07:40:08.329: E/AndroidRuntime(1794):     ... 20 more
09-24 07:40:08.329: E/AndroidRuntime(1794): Caused by: java.lang.ClassCastException
09-24 07:40:08.329: E/AndroidRuntime(1794):     ... 25 more
3
  • 1
    Can you post the error? That could help track down where the bug is. Commented Sep 24, 2015 at 7:38
  • I've updated my question Commented Sep 24, 2015 at 7:51
  • The error is in xml, post R.layout.activity_main Commented Sep 24, 2015 at 7:52

2 Answers 2

1

problem is that you set your api level too low, so it use "support" version of fragment, instead of "base" fragment

put "min api level" at 11 in your androidmanifest, remove dependency to "support" library, remove "import android.support.v4.app.Fragment" and replace it with "import android.app.fragment" and it should work

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

Comments

0

change

public class MainActivity extends Activity {  

to

public class MainActivity extends FragmentActivity {

Comments

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.