ActionBarSherlock SherlockFragmentActivity に関して問題があります。私が使用している目的でナビゲーション ドロワーを作成するためにこのガイドに従おうとしましたが、従うべき別の適切なガイドを見つけることができませんでした (複数の 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 の次の項目で「シンボルを解決できません」というメッセージが表示されます。
mTitle と mSubTitle
// Declare Variables
Context context;
String{} mTitle;
String{} mSubTitle;
int[] mIcon;
LayoutInflater inflater;
再び mTitle と mSubtitle (およびこれに続く 2 つの他のすべてのインスタンス)
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;
}
}
スペースを節約するために、適切なフラグメント (R.layout.fragment1、container、false) を反映する inflater.inflate を除いて、すべて同じコードを持つ Fragment1.java、Fragment2.java、Fragment3.java の 3 つのフラグメント Java ファイルがあります。 ) などなので、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>