0

This is my first time run with Fragments. Have a look at the below code.

activity_main.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <fragment 
        android:id="@+id/firstViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.example.fragmenttest.FirstView"/>

    <fragment 
        android:id="@+id/secondViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.example.fragmenttest.SecondView"/>

</RelativeLayout>

fragment_view1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/viewOneText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="92dp"
        android:layout_marginTop="182dp"
        android:text="First View"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/viewOneBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:text="Click Here" />

</RelativeLayout>

fragment_view2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffddff" >

    <TextView
        android:id="@+id/secondViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="190dp"
        android:text="View 2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/secondViewBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/secondViewText"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

MainActivity.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

FirstView.java

package com.example.fragmenttest;

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

public class FirstView extends Fragment
{
    private TextView firstText;
    private Button btn;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        firstText = (TextView)getView().findViewById(R.id.viewOneText);
        btn = (Button)getView().findViewById(R.id.viewOneBtn);

        btn.setOnClickListener(new ButtonEvent());

        View view = inflater.inflate(R.layout.fragment_view1,container,false);
        return view;

    }

    private class ButtonEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            firstText.setText("First View Btn Clicked");

        }

    }
}

SecondView.java

package com.example.fragmenttest;

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

public class SecondView extends Fragment
{
    private TextView secondText;
    private Button secondViewBtn;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        secondText = (TextView)getView().findViewById(R.id.secondViewText);
        secondViewBtn = (Button)getView().findViewById(R.id.secondViewBtn);

        secondViewBtn.setOnClickListener(new ButtonEvent());

        View view = inflater.inflate(R.layout.fragment_view2,container,false);
        return view;

    }

    private class ButtonEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            secondText.setText("Second View Text changed");

        }

    }
}

When I run this application, I get the following error

11-26 12:18:36.137: E/AndroidRuntime(850): FATAL EXCEPTION: main
11-26 12:18:36.137: E/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmenttest/com.example.fragmenttest.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.os.Looper.loop(Looper.java:137)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-26 12:18:36.137: E/AndroidRuntime(850):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 12:18:36.137: E/AndroidRuntime(850):  at java.lang.reflect.Method.invoke(Method.java:511)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-26 12:18:36.137: E/AndroidRuntime(850):  at dalvik.system.NativeStart.main(Native Method)
11-26 12:18:36.137: E/AndroidRuntime(850): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Activity.setContentView(Activity.java:1881)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.example.fragmenttest.MainActivity.onCreate(MainActivity.java:12)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Activity.performCreate(Activity.java:5104)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-26 12:18:36.137: E/AndroidRuntime(850):  ... 11 more
11-26 12:18:36.137: E/AndroidRuntime(850): Caused by: java.lang.ClassCastException: com.example.fragmenttest.FirstView cannot be cast to android.app.Fragment
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Fragment.instantiate(Fragment.java:585)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Fragment.instantiate(Fragment.java:560)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Activity.onCreateView(Activity.java:4709)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
11-26 12:18:36.137: E/AndroidRuntime(850):  ... 21 more

Why am I unable to run this?

UPDATE

As SO users mentioned, I changed my MainActivity

package com.example.fragmenttest;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Now I have the following error

11-26 12:35:34.137: E/AndroidRuntime(968): FATAL EXCEPTION: main
11-26 12:35:34.137: E/AndroidRuntime(968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmenttest/com.example.fragmenttest.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.os.Looper.loop(Looper.java:137)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-26 12:35:34.137: E/AndroidRuntime(968):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 12:35:34.137: E/AndroidRuntime(968):  at java.lang.reflect.Method.invoke(Method.java:511)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-26 12:35:34.137: E/AndroidRuntime(968):  at dalvik.system.NativeStart.main(Native Method)
11-26 12:35:34.137: E/AndroidRuntime(968): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.Activity.setContentView(Activity.java:1881)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.example.fragmenttest.MainActivity.onCreate(MainActivity.java:13)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.Activity.performCreate(Activity.java:5104)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-26 12:35:34.137: E/AndroidRuntime(968):  ... 11 more
11-26 12:35:34.137: E/AndroidRuntime(968): Caused by: java.lang.NullPointerException
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.example.fragmenttest.FirstView.onCreateView(FirstView.java:19)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-26 12:35:34.137: E/AndroidRuntime(968):  ... 21 more
halfer
  • 19,824
  • 17
  • 99
  • 186
PeakGen
  • 21,894
  • 86
  • 261
  • 463

5 Answers5

2

at least, MainActivity should extend FragmentActivity.

After that in your fragments:

This code:

firstText = (TextView)getView().findViewById(R.id.viewOneText);
btn = (Button)getView().findViewById(R.id.viewOneBtn);

btn.setOnClickListener(new ButtonEvent());

should go after you inflate anything:

View view = inflater.inflate(R.layout.fragment_view1,container,false);

Please, return to the fragments docs to understand them deeper. Now it seems you don't have more or less clear vision of how to use fragments. Your questions will persist until you get fragments know better. Good luck and welcome back if you have any issues.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
1

You are using a mix of Fragment from Android SDK 11 and support Fragments.

There are 2 solutions :

  • as @Vladimir Ivanov suggested, you could use a FragmentActivity (which is the support Activity that deals with support Fragments)
  • you could use a android.app.Fragment (which is the Android SDK 11+ Fragment that goes well with support andorid.app.Activity of SDK 11+)

This is really a bit confusing, as some classes share the same name. But you must decide either to use Android SDK 11+ or the support library to support devices from SDK 7+. You can't mix those classes.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
1

Your MainActivity must be extend FragmentActivity from the support library..

And in your onCreateView() of first fragment this is the first line

    View view = inflater.inflate(R.layout.fragment_view1,container,false);

and replace getView() with view

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
1

You are attempting to cast a SupportMapFragment to be a native API Level 11 android.app.Fragment. That will not work. Either:

Set your android:minSdkVersion to 11 or higher.

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

the question is simple.you should create FragmentManager and FragmentTransaction is constance to manager fragment