2015-01-20 68 views
0

我正在使用這個庫slidingmenu https://github.com/jfeinstein10/SlidingMenu 當我想要添加listview到滑動菜單,我使用此代碼java.lang.IllegalArgumentException:沒有找到ID爲0x7f030018(佈局/列表)的片段SampleListFragment {2a6da765#0 id = 0x7f030018}

getSupportFragmentManager() 
      .beginTransaction() 
      .replace(R.layout.list, new SampleListFragment()) 
      .commit(); 

,但它給了我這個 異常java.lang.IllegalArgumentException異常:對於片段SampleListFragment發現ID 0x7f030018(佈局/列表)無景觀{2a6da765#0 ID = 0x7f030018}

public class MainActivity extends SherlockFragmentActivity { 
    private com.actionbarsherlock.app.ActionBar action; 
     SlidingMenu sm; 
     TextView txt_1,txt_2,txt_3,txt_4,txt_5; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     //requestWindowFeature(Window.FEATURE_NO_TITLE); 
     //getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 
     setContentView(R.layout.activity_main); 

     /*SlidingMenu menu; 
     menu = new SlidingMenu(this); 
     menu.setMode(SlidingMenu.LEFT); 
     menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 
     menu.setShadowWidth(100); 
     menu.setFadeDegree(0.0f); 
     menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); 
     menu.setBehindWidth(200); 
     menu.setMenu(R.layout.menu_frame);*/ 
     txt_1=(TextView)findViewById(R.id.txt_1); 
     txt_1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "ttkkkk", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     txt_2=(TextView)findViewById(R.id.txt_2); 
     txt_2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "ttkkkk1", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     txt_3=(TextView)findViewById(R.id.txt_3); 
     txt_3.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "ttkkkk3", Toast.LENGTH_LONG).show(); 

      } 
     }); 
     txt_4=(TextView)findViewById(R.id.txt_4); 
     txt_4.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "ttkkkk4", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     txt_5=(TextView)findViewById(R.id.txt_5); 
     txt_5.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "ttkkkk5", Toast.LENGTH_LONG).show(); 

      } 
     }); 

      sm = new SlidingMenu(this); 

      sm.setShadowWidthRes(R.dimen.shadow_width); 
      sm.setShadowDrawable(R.drawable.shadow); 
      sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); 
      sm.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); 
      sm.setFadeDegree(0.35f); 
      sm.setMode(SlidingMenu.LEFT); 
      sm.setMenu(R.layout.list); 

      getSupportFragmentManager() 
      .beginTransaction() 
      .replace(R.layout.list, new SampleListFragment()) 
      .commit(); 

      //sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 
      sm.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show(); 

       } 
      }); 

      action = getSupportActionBar(); 
       // action.setDisplayHomeAsUpEnabled(false); 
       // action.setDisplayShowHomeEnabled(false); 
      action.setDisplayShowTitleEnabled(false); 

       action.setHomeButtonEnabled(true); 

       action.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#343333"))); 
      // action.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#66ffffff"))); 
       action.setIcon(R.drawable.menu); 
      // getSupportActionBar().setDisplayShowCustomEnabled(true); 
       // action.setCustomView(R.layout.actionbar); 

      // getSupportActionBar().setCustomView(
      //   R.layout.actionbar); 
      // action.setIcon(R.drawable.menu); 

      // getActionBar().setIcon(
         // new ColorDrawable(getResources().getColor(android.R.color.transparent))); 

      // action.setCustomView(view); 







    } 




    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      sm.toggle(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 






public class SampleListFragment extends ListFragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.list, 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.row, 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; 
     } 

    } 
} 

list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" 
    android:id="@+id/lin" 

    > 

    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="280dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp"  
     android:listSelector="@drawable/list_selector" 
     android:background="@color/list_background"/> 

</LinearLayout> 

row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/row_icon" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:padding="10dp" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/row_title" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center_vertical" 
     android:padding="10dp" 
     android:text="Medium Text" 
     /> 

</LinearLayout> 

回答

0

你需要有碎片的容器來取代它。您可以使用的幀結構的集裝箱像這個 -

<FrameLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

你可以,如果你的活動設置有這個XML佈局或片段充有含佈局這個容器由片斷交易添加您的片段框架佈局/容器。

然後你可以使用代碼 -

getSupportFragmentManager() 
     .beginTransaction() 
     .replace(R.id.container, new SampleListFragment()) 
     .commit(); 

如果您正在尋找,然後使用DrawerLayout是不錯的選擇,導航抽屜式活動。你可以看看here

+0

TNX我使用它,但不解決了這個問題 – 2015-01-20 22:07:08

+0

爲什麼你在尋找這個片段? 您正在使用已在抽屜側具有正視圖的庫。 在哪裏添加另一個片段作爲列表? – 2015-01-20 22:16:59

0

NOOO!在list.xml應該返回到片段作爲其rootview

@Override 
public View onCreateView(LayoutInflater inflater, 
     @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    return inflater.inflate(R.layout.list, container); 
} 

這個代碼.replace(R.id.i_am_suppose_to_refer_t0_a_viewgroup(LinearLayout or something), new SampleListFragment())activity_main.xml佈局文件視圖佈局..查看文檔的更多信息

相關問題