我在ActionBarSherlock SherlockFragmentActivity方面遇到了麻烦。我试图关注this guide以便为我正在使用的目的创建导航抽屉,但我无法找到另一个适用的指南(已经进行了多次Google搜索,搜索了SO和其他论坛等。)所以我来这里请求任何帮助,因为这是一个非常重要的项目,我一直在负责。任何帮助都将非常赞赏,因为我仍处于Android编程的学习阶段,目前无法找到其他解决方案。请帮忙!!谢谢!
我遇到的问题是:
1。)在MainLoader.java中,我在MainLoader.java中遇到以下“无法解析符号”:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main_loader, menu);
return true;
}
// The click listener for ListView in the navigation drawer
private class DrawerItemClickListener implements
ListView.onItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectedItem(position);
}
}
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.main, fragment1);
break;
case 1:
ft.replace(R.id.main, fragment2);
break;
case 2:
ft.replace(R.id.main, fragment3);
break;
}
ft.commit();
mDrawerList.setItemChecked(position, true);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
2。)我还在MenuListAdapter.java中收到以下“无法解析符号”:
// Declare Variables
Context context;
String{} mTitle;
String{} mSubTitle;
int[] mIcon;
LayoutInflater inflater;
mTitle和mSubtitle(以及此后的两个其他实例)
public MenuListAdapter(MainLoader context, String[] title, String[] subtitle,
int[] icon) {
this.context = context;
this.mTitle = title;
this.mSubTitle = subtitle;
this.mIcon = icon;
}
这是我的MainLoader.java
package com.marywood;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.*;
import android.app.ActionBar;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.support.v4.view.GravityCompat;
import static com.marywood.R.drawable;
public class MainLoader extends SherlockFragmentActivity {
// Declare Variable
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
String[] subtitle;
int[] icon;
Fragment fragment1 = new Fragment1(); // Define Fragment 1
Fragment fragment2 = new Fragment2(); // Define Fragment 2
Fragment fragment3 = new Fragment3(); // Define Fragment 3
// Action Bar error fix
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_loader);
// Generate title
title = new String[] { "Home", "News & Events", "Admissions"};
// Generate subtitle
subtitle = new String[] {};
// Generate icon
icon = new int[] { drawable.ic_magnifying_glass, drawable.ic_refresh, drawable.ic_compose};
// Locate DrawerLayout in main_loader.xml
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Locate ListView in drawer_layout.xml
mDrawerList = (ListView) findViewById(R.id.MainList);
// Set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(drawable.drawer_shadow, GravityCompat.START);
// Pass results to MenuListAdapter Class
mMenuAdapter = new MenuListAdapter(this, title, subtitle, icon);
// Set the MenuListAdapter to the ListView
mDrawerList.setAdapter(mMenuAdapter);
// Capture button clicks on side menu
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//Enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main_loader, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
// The click listener for ListView in the navigation drawer
private class DrawerItemClickListener implements
ListView.onItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectedItem(position);
}
}
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.main, fragment1);
break;
case 1:
ft.replace(R.id.main, fragment2);
break;
case 2:
ft.replace(R.id.main, fragment3);
break;
}
ft.commit();
mDrawerList.setItemChecked(position, true);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
这是我的main_loader.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/drawer_layout"
android:layout_width="400dp"
android:layout_height="600dp" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#015A20"/>
<ListView
android:id="@+id/MainList"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#850736"
android:textColor="#FBA81A"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
drawer_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:orientation="vertical" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/subtitle"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
MenuListAdapter.java
package com.marywood;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MenuListAdapter extends BaseAdapter {
// Declare Variables
Context context;
String{} mTitle;
String{} mSubTitle;
int[] mIcon;
LayoutInflater inflater;
public MenuListAdapter(Context context, String[] title, String[] subtitle,
int[] icon) {
this.context = context;
this.mTitle = title;
this.mSubTitle = subtitle;
this.mIcon = icon;
}
@Override
public int getCount() {
return mTitle.length;
}
@Override
public Object getItem(int position) {
return mTitle[position];
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare variables
TextView txtTitle;
TextView txtSubTitle;
ImageView imgIcon;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.drawer_list_item, parent, false);
// Locate the TextViews in drawer_list_item.xml
txtTitle = (TextView) itemView.findViewById(R.id.title);
txtSubTitle = (TextView) itemView.findViewById(R.id.subtitle);
// Locate the ImageView in drawer_list_item.xml
imgIcon = (ImageView) itemView.findViewById(R.id.icon);
// Set the results into TextViews
txtTitle.setText(mTitle[position]);
txtSubTitle.setText(mSubTitle[position]);
return itemView;
}
}
为了节省空间,我有3个片段java文件:Fragment1.java,Fragment2.java和Fragment3.java,它们都有相同的代码,除了反映正确片段的inflater.inflate(R.layout.fragment1,容器,假)等所以我只是把Fragment1.java代码:
package com.marywood;
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
}
同样的情况下,片段XML文件都具有相同的布局但节省空间我将显示fragment1.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/Fragment1" />
</RelativeLayout>
最后是strings.xml资源文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Marywood Mobile</string>
<string name="menu_settings">Settings</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="Fragment1">This is Fragment 1</string>
<string name="Fragment2">This is Fragment 2</string>
<string name="Fragment3">This is Fragment 3</string>
</resources>
答案 0 :(得分:0)
事实证明,我实际上意外地导入了两个相同的类(即标准app类和同时支持.v4类。恢复这些更改解决了错误。