Search code examples
aide-ide

Compile error with two different fragmenttransaction types


I'm using AIDE on my phone, and trying to open a fragment from the navigation drawer in-app, but I get this error when I try to build the app:

An instance of type 'android.app.fragmenttransaction' cannot be assigned to a variable of type 'android.support.v4.app.fragmenttransaction'

Here is the code in the MainActivity.Java file:

package com.nickdgreen.net.act1;

import android.content.*;
import android.content.res.*;
import android.os.*;
import android.support.v4.app.*;
import android.support.v4.widget.*;
import android.view.*;
import android.widget.*;
import android.app.ActionBar.*;
import android.app.Activity.*;
import android.content.res.Configuration.*;
import android.os.Bundle.*;
import android.support.v4.app.ActionBarDrawerToggle.*;
import android.support.v4.app.Fragment.*;
import android.support.v4.app.FragmentActivity.*;
import android.support.v4.app.FragmentManager.*;
import android.support.v4.app.FragmentTransaction.*;
import android.support.v4.widget.DrawerLayout.*;
import android.view.Menu.*;
import android.view.MenuInflater.*;
import android.view.MenuItem.*;
import android.view.View.*;
import android.widget.AdapterView.*;
import android.widget.ArrayAdapter.*;
import android.widget.ListView.*;

public class MainActivity extends FragmentActivity
{

private ActionBarDrawerToggle drawerToggle;
final String fragments[] = {
    "com.nickdgreen.net.act1.MainFragment", 
    "com.nickdgreen.net.act1.OneFragment", 
    "com.nickdgreen.net.act1.TwoFragment",
    "com.nickdgreen.net.act1.ThreeFragment",

};
final String menuEntries[] = {
    "Main", "One", "Two", "Three"
};

public MainActivity()
{
}

public void onConfigurationChanged(Configuration configuration)
{
    super.onConfigurationChanged(configuration);
    drawerToggle.onConfigurationChanged(configuration);
}

protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(0x7f030000);
    ArrayAdapter arrayadapter = new ArrayAdapter(getActionBar().getThemedContext(), 0x1090003, menuEntries);
    final DrawerLayout drawer = (DrawerLayout)findViewById(0x7f080000);
    final ListView navList = (ListView)findViewById(0x7f080002);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    drawerToggle = new ActionBarDrawerToggle(this, drawer, 0x7f020000, 0x7f050003, 0x7f050002) {

        final MainActivity this$0;

        public void onDrawerClosed(View view)
        {
        }

        public void onDrawerOpened(View view)
        {
        }


        {
            this$0 = MainActivity.this;

        }
    };
    drawer.setDrawerListener(drawerToggle);
    navList.setAdapter(arrayadapter);
    navList.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        final MainActivity this$0;
        final DrawerLayout val$drawer;
        final ListView val$navList;

        public void onItemClick(AdapterView adapterview, View view, int i, long l)
        { {

                }

            drawer.closeDrawer(navList);
        }


        {

        }
    });
    FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
    fragmenttransaction.replace(0x7f080001, Fragment.instantiate(this, fragments[0]));
    fragmenttransaction.commit();
}





public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(0x7f070000, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem menuitem)
{
    if (drawerToggle.onOptionsItemSelected(menuitem))
    {
        return true;
    } else
    {
        return super.onOptionsItemSelected(menuitem);
    }
}

protected void onPostCreate(Bundle bundle)
{
    super.onPostCreate(bundle);
    drawerToggle.syncState();}




private class DrawerItemClickListener 
implements ListView.OnItemClickListener {

    @Override 

    public void onItemClick(AdapterView parent, View view, int position, long id) 
    { selectItem(position); } 

    /** Swaps fragments in the main content view */

    private void 

    selectItem(int position) {

        //Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); // args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); 
        Intent intent = new Intent(MainActivity.this, OneFragment.class); startActivity(intent); } }

        public class 
        ProductListActivity extends MainActivity { 

        @Override 
        public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate main_menu.xml 
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch (item.getItemId())
            {
                case R.id.mainMenuAbout:

                    Intent intent = new Intent(this, AboutActivity.class); 
                    startActivity(intent); 
                    return true;
                case R.id.mainMenuExit:
                    finish();
                    return true;

            }
            return super.onOptionsItemSelected(item);
        }
        @Override public void onClick(Bundle b) { super.onCreate(b); setContentView(R.layout.primary);}

        public void 

        selectItem(int position) { 

               Fragment newFragment;
               FragmentTransaction transaction = getFragmentManager().beginTransaction();

               switch (position) {

                   case 0: 
                       newFragment = new OneFragment(); 
                       transaction.replace(R.id.content_frame, newFragment); 
                       transaction.addToBackStack(null); transaction.commit();
                       break; 

                   case 1: 
                       newFragment = new TwoFragment();
                       transaction.replace(R.id.content_frame, newFragment); 
                       transaction.addToBackStack(null);
                       transaction.commit(); 
                       break; 

                   case 2: 
                       newFragment = new ThreeFragment();
                       transaction.replace(R.id.content_frame, newFragment); 
                       transaction.addToBackStack(null); 
                       transaction.commit(); 
                       break; 

                   case 3: 

                       newFragment = new FourFragment(); 
                       transaction.replace(R.id.content_frame, newFragment); 
                       transaction.addToBackStack(null);
                       transaction.commit(); 
                       break; 

                       }

                       //DrawerList.setItemChecked(position, true);
            CharSequence[] ListTitles = null;
            setTitle(ListTitles[position]); 
            View DrawerList = null;
        }
    }   
}

Solution

  • import android.support.v4.app.FragmentTransaction.*;
    

    This line means that you are using FragmentTransaction; note that you are using the support lib's version, not the system's.

    However, later in the code, you are assigning the support lib's FragmentTransaction to the native FragmentTransaction. They are incompatible and thus won't work. (Actually, it's what the error message itself is telling you.)

    FragmentTransaction transaction   = getFragmentManager().beginTransaction();
    // ^^^ support lib's version        ^^^ this code returns a native FragmentTransaction
    

    You should instead get the support lib's fragment manager, which will return the support FragmentTransaction.