2013-08-05 39 views
0

我正在开发一个聊天应用程序,并且想要添加一个像GMail应用程序中的导航抽屉。我是新来的Java所以请不要杀我:DI试图添加它,但它崩溃:(Android导航抽屉失败

这是我的一些Chat.java的代码

public class Chat extends FragmentActivity { 

final String[] data ={"Login","Register"}; 
final String[] fragments ={ 
     "com.linkr.chat.Login", 
     "com.linkr.chat.Register"}; 
String username; 
BufferedReader reader; 
PrintWriter writer; 
ArrayList<String> userList = new ArrayList(); 
public TextView chatTextArea; 
TextView inputTextArea; 
Button onClickButton; 
Boolean isConnected = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setTitle("Linkr"); 
    /*<-- NAVDRAWER -->*/ 
    setContentView(layout.activity_chat); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data); 
    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); 
    final ListView navList = (ListView) findViewById(R.id.drawer); 
    navList.setAdapter(adapter); 
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){ 
      drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){ 
       @Override 
       public void onDrawerClosed(View drawerView){ 
        super.onDrawerClosed(drawerView); 
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
        tx.replace(R.id.main, Fragment.instantiate(Chat.this, fragments[pos])); 
        tx.commit(); 
       } 
      }); 
      drawer.closeDrawer(navList); 
     } 
    }); 
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
    tx.replace(R.id.main,Fragment.instantiate(Chat.this, fragments[0])); 
    tx.commit(); 
    /*<-- NAVDRAWER END -->*/ 

我也不要”吨得到如何使用NavigationDrawer

这是我的activity_chat.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="300dp" 
      android:layout_height="800dp" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      tools:context=".Chat" 
      android:text = "Linkr"> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
             xmlns:tools="http://schemas.android.com/tools" 
             android:id="@+id/drawer_layout" 
             android:layout_width="300dp" 
             android:layout_height="800dp" 
             tools:context=".MainActivity" > 

    <ListView 
      android:id="@+id/drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="#FFF" 
      android:choiceMode="singleChoice"/> 

</android.support.v4.widget.DrawerLayout> 

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/inputTextArea" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/onClickButton"/> 

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/onClickButton" 
     android:layout_alignBaseline="@+id/inputTextArea" 
     android:layout_alignBottom="@+id/inputTextArea" 
     android:layout_alignParentRight="true"/> 

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" 
     android:ems="10" 
     android:id="@+id/chatTextArea" 
     android:layout_above="@+id/onClickButton" 
     android:layout_alignLeft="@+id/inputTextArea" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/onClickButton"/> 

我是否需要anoth呃它的XML?有人能告诉我这是如何工作的吗? 谢谢你们! :)

+2

发布错误 – tyczj

+0

的堆栈跟踪我没有得到任何错误:( – calmandniceperson

+0

你说崩溃 – tyczj

回答

1

android.support.v4.widget.DrawerLayout被设计为top ViewGroup,可以有两个孩子。第一个是你的Content View(可见的视图),就像正常的活动布局。第二个是你的Drawer View(你可以从侧面拉视图)。

内容视图和抽屉视图都可以是一个ViewGroup像一个RelativeLayout或一个视图在你的情况下ListView。

所以你的布局应该是这样的:

<android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/drawer_layout" 
     android:layout_width="300dp" 
     android:layout_height="800dp" 
     tools:context=".MainActivity" > 

     <!-- YOUR CONTENT VIEW--> 
      <RelativeLayout 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="300dp" 
       android:layout_height="800dp" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       tools:context=".Chat" 
       android:text = "Linkr"> 
     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/inputTextArea" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_toLeftOf="@+id/onClickButton"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Send" 
      android:id="@+id/onClickButton" 
      android:layout_alignBaseline="@+id/inputTextArea" 
      android:layout_alignBottom="@+id/inputTextArea" 
      android:layout_alignParentRight="true"/> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="textMultiLine" 
      android:ems="10" 
      android:id="@+id/chatTextArea" 
      android:layout_above="@+id/onClickButton" 
      android:layout_alignLeft="@+id/inputTextArea" 
      android:layout_alignParentTop="true" 
      android:layout_alignRight="@+id/onClickButton"/> 
     </RelativeLayout> 

     <--! YOUR DRAWER VIEW --> 
     <ListView 
       android:id="@+id/drawer" 
       android:layout_width="240dp" 
       android:layout_height="match_parent" 
       android:layout_gravity="start" 
       android:background="#FFF" 
       android:choiceMode="singleChoice"/> 

    </android.support.v4.widget.DrawerLayout> 
+0

好吧我改变了,谢谢你:D但它仍然崩溃 – calmandniceperson

+0

@Steve而不是listview我正在使用Linearlayout其崩溃为08 -12 java.lang.IllegalArgumentException:View android.widget.LinearLayout {41971398 VE..C。... P ... D 0,0-480,762#7f070053 app:id/parent_drawer}不是滑动抽屉 –

+0

@PadmaKumar你可以用你的问题开一个新的问题吗?并请与更多的细节;)。 –