Android 在onclick事件中重放片段时,替换方法中出现导航抽屉错误
我正在尝试实现navigtion drawer这是我的代码,运行应用程序时出错 这是eclipse问题中的错误 说明资源路径位置类型 FragmentTransaction类型中的replace(int,Fragment)方法不适用于参数(int,Fragment)MainActivity.java/NavigationDrawer/src/com/example/NavigationDrawer line 198 java Problem 这是我的主要活动代码Android 在onclick事件中重放片段时,替换方法中出现导航抽屉错误,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我正在尝试实现navigtion drawer这是我的代码,运行应用程序时出错 这是eclipse问题中的错误 说明资源路径位置类型 FragmentTransaction类型中的replace(int,Fragment)方法不适用于参数(int,Fragment)MainActivity.java/NavigationDrawer/src/com/example/NavigationDrawer line 198 java Problem 这是我的主要活动代码 package com.
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");
}
}
}
这是我的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>
尝试使用
片段活动
代替活动
public class MainActivity extends FragmentActivity
public class MainActivity extends FragmentActivity
及
我也犯了同样的错误。需要修改两个位置,如下所示 首先,使用
FragmentActivity
public class MainActivity extends FragmentActivity
public class MainActivity extends FragmentActivity
其次,将getFragmentManager
更改为getSupportFragmentManager
:
FragmentManager fragmentManager = getSupportFragmentManager();
为什么呢?如果使用支持库中的片段,则应使用FragmentActivity。并查看导入
import android.app.Fragment代码>因为您使用的是片段。错误,您可以扩展活动,也可以使用片段。检查我在上一篇评论中发布的lin抱歉,你是对的,但这将是针对API 11或更高版本的,这是不对的。他是extendign活动,所以他的目标是API 11级及以上。如果低于11,他应该使用appcomapt作为支持库中提供的操作栏,在这种情况下,您可以扩展ActionBarActivity。因此,您的答案对op没有帮助。请在displayView方法中尝试此链接,将您的FragmentManager替换为android.support.v4.app.FragmentManager,并将FragmentTransaction替换为android.support.v4.app.FragmentTransaction,并使用getSupportFragmentManager方法代替getFragmentManager。如果您使用FragmentActivity¬ Activity扩展您的类,它将可用。如果你需要更多的澄清,请告诉我。