Incompatibility between Fragment and ListFragment

106 Views Asked by At

Note : I've looked at questions similar to this. Most of them said to interchange between import android.app.ListFragment and import android.support.v4.app.ListFragment; However this didn't solve my problem.

I've made a Fragment called fragmentOne. I've tried to implement a list in fragmentOne using ListFragment. When a particular button is clicked, FragmentOne should appear on screen. The method to be called when the button for fragmentOne is pressed, is FragmentOneClick(View v). It is in MainActivity.java. However, in this method, in the line:

  Fragment myFragment=new fragmentTwo();

I'm getting an error saying:

incompatible types. Required : android.app.ListFragment found: com.example.projectName.fragmentOne

How can I resolve this error?

fragmentOne.java:

import android.app.ListFragment;
import android.content.Context;
import android.net.Uri; 
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


public class fragmentOne extends android.support.v4.app.ListFragment
{
    String dates[]={"1","2","3","4","5","6","7","8","9"};


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

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,dates);
        setListAdapter(adapter);
    }
}

FragmentOne.xml:

<FrameLayout 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"
tools:context="com.example.nirvan.fragment_example.fragmentOne">

<!-- TODO: Update blank fragment layout -->

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list"
    android:layout_gravity="left|top" />


</FrameLayout>

MainActivity.java:

import android.support.v4.app.Fragment;
import android.app.ListFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.view.View;


public class MainActivity extends AppCompatActivity{

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

    public void FragmentOneClick(View v)
    {
        ListFragment myFragment=new fragmentOne();
        FragmentManager fm= getFragmentManager();

        FragmentTransaction ft=fm.beginTransaction();
        ft.replace(R.id.fragment_switch, (android.app.Fragment) myFragment );
        ft.commit();
    }

    public void FragmentTwoClick(View v)
    {
        Fragment myFragment=new fragmentTwo();
        FragmentManager fm= getFragmentManager();

        FragmentTransaction ft=fm.beginTransaction();
        ft.replace(R.id.fragment_switch, myFragment);
        ft.commit();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.nirvan.fragment_example.MainActivity">

    <Button
        android:onClick="FragmentOneClick"
    android:text="Fragment1"
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="150dp"
        android:layout_weight="2"
    >
    </Button>

<Button
    android:text="Fragment2"
    android:id="@+id/button2"
    android:onClick="FragmentTwoClick"
    android:layout_height="wrap_content"
    android:layout_width="150dp"
    android:layout_alignTop="@+id/button1"
    android:layout_alignRight="@+id/fragment_switch"
    android:layout_alignEnd="@+id/fragment_switch">
    android:layout_weight="2"
</Button>

<fragment
    android:id="@+id/fragment_switch"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_below="@+id/button1"
    android:name="com.example.nirvan.fragment_example.fragmentOne"
    tools:layout="@layout/fragment_fragment_one" />

</RelativeLayout>
0

There are 0 best solutions below