奇怪的是,我的程序中有2个列表视图。第一个列表视图我设置android:id =“@ android:id / list” 和第二个listview我的id我设置android:id =“@ + id / list1”。
这是正确的方法吗?因为我将第二个listview id设置为android:id =“@ + id / list1”所以无法运行所以我想知道它是因为它是id错误还是我的代码中存在问题。
我正在使用带有actionbarsherlock的jeremy库。我只是按照杰里米的例子,我没有找到他使用任何listView findviewbyid(列表)来获取他的菜单。在我改变id后,我的应用程序崩溃了。 感谢
rightlist.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listright"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />
leftlist.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listleft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />
LeftListFragment.java
public class LeftListFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.leftlist, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem("a"));
adapter.add(new SampleItem("b"));
adapter.add(new SampleItem("c"));
adapter.add(new SampleItem("d"));
adapter.add(new SampleItem("e"));
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public SampleItem(String tag) {
this.tag = tag;
}
}
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.leftrow, null);
}
TextView title = (TextView) convertView.findViewById(R.id.row_title1);
title.setText(getItem(position).tag);
return convertView;
}
}
}
rightlistfragment.java
public class RightListFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.rightlist, 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.rightrow, 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;
}
}
}
主要活动/ leftandrightactivity.java
public class LeftAndRightActivity extends BaseActivity {
public LeftAndRightActivity() {
super(R.string.left_and_right);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setContentView(R.layout.activity_main);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new LeftListFragment())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new RightListFragment())
.commit();
}
}
错误日志
10-29 17:06:52.085: E/AndroidRuntime(26618): FATAL EXCEPTION: main
10-29 17:06:52.085: E/AndroidRuntime(26618): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peopeo.peopeo1/com.peopeo.peopeo1.LeftAndRightActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.os.Looper.loop(Looper.java:137)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-29 17:06:52.085: E/AndroidRuntime(26618): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 17:06:52.085: E/AndroidRuntime(26618): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 17:06:52.085: E/AndroidRuntime(26618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-29 17:06:52.085: E/AndroidRuntime(26618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-29 17:06:52.085: E/AndroidRuntime(26618): at dalvik.system.NativeStart.main(Native Method)
10-29 17:06:52.085: E/AndroidRuntime(26618): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1167)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.Activity.performStart(Activity.java:5216)
10-29 17:06:52.085: E/AndroidRuntime(26618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
10-29 17:06:52.085: E/AndroidRuntime(26618): ... 11 more
答案 0 :(得分:2)
我同意@NitroNbg,你应该指定你的新ID。喜欢 android:id =“@ + id / listId”
ListView list = (ListView) findViewById(R.id.listId);
或检查this一个。
答案 1 :(得分:1)
ListFragment
期望布局包含ListView
,其ID为@android:id/list
:
ListFragment具有由单个列表视图组成的默认布局。但是,如果需要,可以通过从
onCreateView(LayoutInflater, ViewGroup, Bundle)
返回自己的视图层次结构来自定义片段布局。为此,您的视图层次结构必须包含一个ID为"@android:id/list"
的ListView对象(如果它在代码中,则为list
)
在不止一个地方拥有相同的ID是完全没问题的。
因此,只需在两种布局中将ListView
ID设置为@android:id/list
。
答案 2 :(得分:0)
您的BaseActivity是否从ListActivity扩展?您的例外情况表明......