2013-10-29 72 views
0

這很奇怪我的程序中有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 
+0

發佈您的代碼。 – blitzen12

+0

完成後,我無法運行它,當我更改ID。但沒有改變就沒有問題 –

+0

你只是改變XML文件中的ID?如果是這樣,那麼你的ListView變量(列表)沒有鏈接到正確的資源。您需要將其初始化更改爲:list =(ListView)findViewById(R.id。list1)'(或任何你的新ID是 – NitroNbg

回答

1

ListFragment預計該佈局包含ListView ID爲@android:id/list

ListFragment具有由單個列表視圖的默認佈局。但是,如果您願意,您可以通過從onCreateView(LayoutInflater, ViewGroup, Bundle)返回自己的視圖層次結構來自定義分段佈局。要做到這一點,您的視圖層次必須包含id爲"@android:id/list"一個ListView對象(或list,如果它在代碼)

這是完全罰款,有相同的ID在多個地方。

因此,只需在兩種佈局中將ListView id設置爲@android:id/list即可。

+0

所以,如果我這樣做,如果我想在左右菜單上點擊偵聽器做不同的事情如何區分左側列表和右側列表? –

+0

@LeowZhengXu在相應片段的'onCreateView()'中設置點擊監聽器。 – laalto

+0

感謝您的幫助,但是我對編程有點新,所以我很感謝你給我更多的解釋(我並不真正明白你的意思,但我已經試過了這個http://stackoverflow.com/questions/19651757/android-list-with-onclicklistener-using-sampleadapter對我的代碼與列表,但它不起作用 –

2

我@NitroNbg同意,應指定新的ID。像機器人:ID = 「@ + ID/listId」

ListView list = (ListView) findViewById(R.id.listId); 

或檢查this之一。

0

您的BaseActivity是從ListActivity擴展的嗎?您的異常,表明...

+0

public class BaseActivity extends SlidingFragmentActivity –

+0

@laalto已經提供瞭解決您的問題的解決方案。 – Alex

相關問題