80,337
社区成员
发帖
与我相关
我的任务
分享
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer;
private ListView drawerList;
private String[] title={"头条","博客","问答","论坛","我的"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer=(DrawerLayout) findViewById(R.id.drawer);
drawerList=(ListView)findViewById(R.id.list);
drawerList.setAdapter(new ArrayAdapter<String>(MainActivity.this ,
android.R.layout.simple_list_item_1 ,title)); //此处报空指针异常
drawerList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"You have click",Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/drawer">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"></FrameLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_gravity="start">
</ListView>
</android.support.v4.widget.DrawerLayout>