很奇怪我的程序中有 2 个列表视图。第一个列表视图我设置了 android:id="@android:id/list",第二个列表视图我设置了 android:id="@+id/list1"。
这是正确的方法吗?因为如果我将我的第二个列表视图 ID 设置为 android:id="@+id/list1" 我无法运行,所以我想知道是因为它是 ID 错误还是我的代码有问题。
我正在使用带有 actionbarsherlock 的 jeremy 库。我只是按照杰里米的例子,我没有找到他使用任何 listView findviewbyid(list) 来获取他的菜单。在我更改 ID 后,我的应用程序崩溃了。谢谢
右列表.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" />
左列表.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