当我试图创建大量值时,我在setAdapter上有NullpointeException 活动主要 公共类MainActivity扩展了AppCompatActivity {
private Toolbar toolbar;
private static final int LAYOUT = R.layout.activiti_main;
private DrawerLayout drawerLayout;
private ViewPager viewPager;
String[] names = { "Иван", "Марья", "Петр", "Антон", "Даша", "Борис",
"Костя", "Игорь", "Анна", "Денис", "Андрей" };
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppDefault);
super.onCreate(savedInstanceState);
setContentView(LAYOUT);
initToolbar();
initNavigationView();
initTabs();
initList();
}
private void initTabs() {
viewPager=(ViewPager)findViewById(R.id.viewPager);
TabLayout tabLayout=(TabLayout) findViewById(R.id.tabLayout);
TabsFragmentAdapter adapter = new TabsFragmentAdapter(getApplicationContext(), getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
private void initToolbar(){
toolbar=(Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name); //наздвание
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { //слушатель события для элементов которые выскакивают в меню
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
toolbar.inflateMenu(R.menu.menu); //создаем менюшку и отсылаемся к файлу меню
}
private void initNavigationView() {
drawerLayout= (DrawerLayout)findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,R.string.text_open,R.string.text_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
NavigationView navView = (NavigationView) findViewById(R.id.navigation);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawerLayout.closeDrawers();
switch (item.getItemId()) {
case R.id.actionHistory: {
viewPager.setCurrentItem(0);
break;
}
case R.id.actionIdea: {
viewPager.setCurrentItem(1);
break;
}
case R.id.actionToDo: {
viewPager.setCurrentItem(2);
break;
}
case R.id.actionBirthday: {
viewPager.setCurrentItem(3);
break;
}
}
return true;
}
});
}
private void initList() {
ListView lvMain = (ListView) findViewById(R.id.lvMain);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names);
lvMain.setAdapter(adapter);
}
listlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello">
</TextView>
<ListView
android:id="@+id/lvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
和ideafragment
public class IdeaFragment extends AbstractTabFragment {
private static final int LAYOUT = R.layout.listlayout;
public static IdeaFragment getInstance(Context context){
Bundle args = new Bundle();
IdeaFragment fragment = new IdeaFragment();
fragment.setArguments(args);
fragment.setContext(context);
fragment.setTitle(context.getString(R.string.tab_item_ideas));
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(LAYOUT,container,false);
return view;
}
public void setContext(Context context) {
this.context = context;
}
}
activiti_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBackground">
<android.support.design.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="6dp"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/white"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_plus"
android:layout_gravity="end|bottom"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_navigation"
app:headerLayout="@layout/navigation_header"
/>
</android.support.v4.widget.DrawerLayout>
但是,正确的上面的lv.setAdapter行会使用空指针异常崩溃应用程序。
有什么想法吗?