0

我在手机上使用 AIDE,并尝试从应用程序内的导航抽屉中打开一个片段,但是当我尝试构建应用程序时出现此错误:

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

这是 MainActivity.Java 文件中的代码:

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;
        }
    }   
}
4

1 回答 1

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

此行表示您正在使用FragmentTransaction; 请注意,您使用的是支持库的版本,而不是系统的版本。

但是,稍后在代码中,您支持库分配FragmentTransaction给本机FragmentTransaction. 它们不兼容,因此不起作用。(实际上,这就是错误消息本身告诉您的内容。)

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

相反,您应该获取支持库的片段管理器,它将返回 support FragmentTransaction

于 2014-07-10T17:45:12.783 回答