我正在尝试在我的应用程序中使用le lib SlidingMenu,但我遇到了一些问题。 我收到了这个错误:
11-04 15:50:46.225:E / FragmentManager(21112):找不到id的视图 片段的0x7f040009(com.myapp:id / menu_frame) SampleListFragment {413805f0#0 id = 0x7f040009}
BaseActivity.java
package com.myapp;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.Menu;
import android.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class BaseActivity extends SlidingFragmentActivity {
private int mTitleRes;
protected ListFragment mFrag;
public BaseActivity(int titleRes) {
mTitleRes = titleRes;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(mTitleRes);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new SampleListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
SlidingMenu slidingMenu = getSlidingMenu();
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
slidingMenu.setFadeDegree(0.35f);
slidingMenu.setMenu(R.layout.slidingmenu);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.myapp.SampleListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
SampleListFragment.java
package com.myapp;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class SampleListFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
for (int i = 0; i < 20; i++) {
adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
}
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
}
MainActivity.java
package com.myapp;
import java.util.ArrayList;
import beans.Tweet;
import database.DatabaseHelper;
import adapters.TweetListViewAdapter;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends BaseActivity {
public MainActivity(){
super(R.string.app_name);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listview_tweets);
DatabaseHelper db = new DatabaseHelper(this);
ArrayList<Tweet> tweets = db.getAllTweets();
TweetListViewAdapter adapter = new TweetListViewAdapter(this, R.layout.listview_item_row, tweets);
listview.setAdapter(adapter);
setSlidingActionBarEnabled(false);
}
}
我不明白为什么找不到视图menu_frame,因为我有一个id为menu_frame的视图,而且这个视图是布局menu_frame的子视图。
答案 0 :(得分:1)
setBehindContentView()
用于设置SlidingMenu
。
/**
* Set the behind view content to an explicit view.
* This view is placed directly into the behind view 's view hierarchy.
* It can itself be a complex view hierarchy.
*
* @param view The desired content to display.
* @param layoutParams Layout parameters for the view. (unused)
*/
public void setBehindContentView(View view, LayoutParams layoutParams) {
mViewBehind = view;
mSlidingMenu.setMenu(mViewBehind);
}
但是,由于在BaseActivity
中您调用了slidingMenu.setMenu(R.layout.slidingmenu);
,因此菜单布局将被覆盖为R.layout.slidingmenu
。因此,R.layout.menu_frame
不会膨胀,也无法找到ID。
尝试删除setBehindContentView()
或setMenu()
。