這很奇怪我的程序中有2個列表視圖。第一個列表視圖我設置了android:id =「@ android:id/list」 和第二個listview我的id我設置了android:id =「@ + id/list1」。當我更改ListView的ID時,無法運行應用程序
這是正確的方法嗎?因爲我不能運行,如果我將我的第二個listview id設置爲android:id =「@ + id/list1」,所以我想知道是因爲它是id錯誤還是代碼中存在問題。
我正在使用帶有actionbarsherlock的jeremy庫。我只是按照傑里米的例子,我沒有找到他使用任何listView findviewbyid(列表)來獲得他的菜單。而我改變後,我的應用程序剛剛墜毀。 由於
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
發佈您的代碼。 – blitzen12
完成後,我無法運行它,當我更改ID。但沒有改變就沒有問題 –
你只是改變XML文件中的ID?如果是這樣,那麼你的ListView變量(列表)沒有鏈接到正確的資源。您需要將其初始化更改爲:list =(ListView)findViewById(R.id。list1)'(或任何你的新ID是 – NitroNbg