プログラムに 2 つのリスト ビューがあるのは奇妙です。最初のリストビューは Android:id="@android:id/list" を設定し、2番目のリストビューは私のIDを Android:id="@+id/list1" に設定しました。
これは正しい方法ですか?2 番目のリストビュー ID を android:id="@+id/list1" に設定すると実行できないため、ID の障害であるか、コードに問題があるためではないかと考えていました。
actionbarsherlock で jeremy ライブラリを使用しています。私は、彼のメニューを取得するために listView findviewbyid(list) を使用して彼を見つけられないジェレミーの例に従っているだけです。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