尝试在Java / Android

时间:2016-06-02 13:38:56

标签: java android android-studio nullpointerexception adapter

(我已经发布了一个关于这个被删除的问题的问题。如果我没有解释好我的问题,我很抱歉。我也是德国人,所以请原谅我,如果我的英语不是很好。)

我真的尝试了很多东西来让这个简单的程序工作。我认为对此的解决方案将非常简单,但首先你需要找到它...... 我尝试了三种不同的方法对此进行编码,最后基于this教程:

  1. 定义变量

    String[] list = {"One", "Two", "Three"};

  2. 创建适配器

    ListAdapter Adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

  3. 将ListView转换为视图

    ListView mainList1 = (ListView) findViewById(R.id.mainList1);

  4. 设置适配器(希望导致问题)

    mainList1.setAdapter(Adapter1);

  5. 因此,每当我尝试设置适配器时,程序都会抛出java.lang.NullPointerException。正如我已经说过的,我尝试了很多方法来使这个程序工作。这就是为什么我现在需要StackOverflow-Comunity的帮助。我想尽可能地解释这个问题所以也许有一天我可以完成我的项目...

    这些信息也可能与此相关:

    完整MainActivity.java (原创)

    public class MainActivity extends AppCompatActivity {
        private SectionsPagerAdapter mSectionsPagerAdapter;
        private ViewPager mViewPager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    
            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);
    
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mViewPager);
    
            String[] Vertretung1 = {
                    "1. Stunde | Frie [Ku] --› Eshk [Et Raum 008]",
                    "2. Stunde | Frie [Ku] --› Eshk [Et Raum 008]",
                    "3. Stunde | Frie [Ku] --› Eshk [Et Raum 008]"
            };
    
            String[] Vertretung2 = {
                    "1. Stunde | Grok [Ma] --› Grot [De Raum 111]",
                    "2. Stunde | Grok [Ma] --› Grot [De Raum 111]",
                    "3. Stunde | Grok [Ma] --› Grot [De Raum 111]"
            };
    
            ListAdapter Adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Vertretung1);
    
            ListView mainList1 = (ListView) findViewById(R.id.mainList1);
    
            mainList1.setAdapter(Adapter1); //Causes the Problem
    
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            if (id == R.id.action_settings) {
                return true;
            }
    
    
    
            return super.onOptionsItemSelected(item);
        }
    
        public static class PlaceholderFragment extends Fragment {
    
            private static final String ARG_SECTION_NUMBER = "section_number";
    
            public PlaceholderFragment() {
            }
    
            public static PlaceholderFragment newInstance(int sectionNumber) {
                PlaceholderFragment fragment = new PlaceholderFragment();
                Bundle args = new Bundle();
                args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                fragment.setArguments(args);
                return fragment;
            }
    
    
        }
    
        public static class FragmentOne extends Fragment{
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main_one, container, false);
    
    
                return rootView;
            }
    
        }
    
        public static class FragmentTwo extends Fragment{
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main_two, container, false);
    
    
                return rootView;
            }
    
        }
    
    
        public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                switch (position){
                    case 0:
                        return new FragmentOne();
                    case 1:
                        return new FragmentTwo();
                    default:
                        return null;
                }
            }
    
    
    
            @Override
            public int getCount() {
                return 2;
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "HEUTE";
                    case 1:
                        return "MORGEN";
                }
                return null;
            }
        }
    }
    

    完整activity_main.xml (原创)

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:context="me.muehl.cvovertretungsplan.MainActivity">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/appbar_padding_top"
                android:theme="@style/AppTheme.AppBarOverlay">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/AppTheme.PopupOverlay">
    
                </android.support.v7.widget.Toolbar>
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        </android.support.design.widget.CoordinatorLayout>
    
    Full `fragment_main_one.xml` *(Original)*: [link][2]
    

    感谢阅读;)

1 个答案:

答案 0 :(得分:1)

您必须将所有ListView事物移动到Fragment类而不是活动中,因为这是您在XML中定义ListView的地方。

ListView mainList1 = (ListView) findViewById(R.id.mainList1);

该行只会搜索您在setContentView中使用的布局。

为了在片段中移动ListView,您可以找到此行

View view = inflater.inflate... 

使用下一行包含...

获取ListView
ListView lv = view.findViewById(...);
// TODO: setAdapter 
return view;