导航抽屉在替换onclick事件中的片段时替换方法出错

时间:2014-01-24 05:37:27

标签: android android-layout android-fragments

我正在尝试实现导航抽屉这是我的代码,我在运行应用程序时遇到错误。

这是日食问题的错误。

描述资源路径位置类型 FragmentTransaction类型中的方法replace(int,Fragment)不适用于参数(int,Fragment)MainActivity.java / NavigationDrawer / src / com / example / navigationdrawer line 198 Java Problem。

这是我的主要活动代码

    package com.example.testnav;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.content.res.Configuration;
    import android.support.v4.app.ActionBarDrawerToggle;

    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

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

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        // 2. App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // 2.1 create ActionBarDrawerToggle
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_launcher,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );

        // 2.2 Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        // 2.3 enable and show "up" arrow
        getActionBar().setDisplayHomeAsUpEnabled(true); 

        // just styling option
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

         // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
        // then it has handled the app icon touch event

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }





    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
           // Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();


            displayView(position);
            drawerLayout.closeDrawer(drawerListView);

        }
    }


    private void displayView(int position) {    

      Fragment fragment = null;
      switch (position) {
      case 0:
          fragment = new FragmentOne();
          break;
      case 1:
          fragment = new FragmentTwo();
          break;
      case 2:
          fragment = new FragmentThree();
          break;
      case 3:
          fragment = new FragmentOne();
          break;
      case 4:
          fragment = new FragmentOne();
          break;
      case 5:
          fragment = new FragmentOne();
          break;

      default:
          break;
      }

      if (fragment != null) {
          FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.beginTransaction()
                  .replace(R.id.content_frame, fragment).commit();

          // update selected item and title, then close the drawer
          drawerListView.setItemChecked(position, true);
          drawerListView.setSelection(position);

            //setTitle((TextView)view).getText());
          drawerLayout.closeDrawer(drawerListView);
      } else {
          // error in creating fragment
          Log.e("MainActivity", "Error in creating fragment");
      }
    }
}

这是片段

    package com.example.testnav;

    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class FragmentOne extends Fragment {

    public FragmentOne(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_one, container, false);

        return rootView;
    }
    }

这是我的xml

   <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"

        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />

    </android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:1)

尝试使用FragmentActivity代替Activity

public class MainActivity extends FragmentActivity

 default:
     fragment = new FragmentOne();
     break;

答案 1 :(得分:0)

我得到了同样的错误。需要修改拖曳地点,如下所示。

首先,使用FragmentActivity

public class MainActivity extends FragmentActivity

其次,将getFragmentManager更改为getSupportFragmentManager

FragmentManager fragmentManager = getSupportFragmentManager();