简体   繁体   中英

Android- I can't make the ListView on second activity

The problem is about when I want to open a second activity. I can't open it, it crashes. But on first activity everything works fine. I tried my best to solve it, but I failed. Here's a code:

Inside 1st Java - onCreate:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    String [] listaDziedzin = { getResources().getString(R.string.astronomia),
            getResources().getString(R.string.dynamika), getResources().getString(R.string.hydrostatyka),
            getResources().getString(R.string.kinematyka), getResources().getString(R.string.optyka),
            getResources().getString(R.string.termodynamika)};


    ListAdapter adapterListDziedzin = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listaDziedzin);

    listaD = (ListView) findViewById(R.id.listView);

    listaD.setAdapter(adapterListDziedzin);

    listaD.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            switch (position) {
                case 0:
                    Intent x = new Intent(MainActivity.this, SecondActivityA.class);
                    startActivity(x);
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }

        }
    });
}

Inside the second one:

 String [] listaKategorii = { getResources().getString(R.string.wzor_newtona),getResources().getString(R.string.trzecie_prawo_keplera),
            getResources().getString(R.string.wzor_ciolkowskiego)};

    ListAdapter v = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listaKategorii);

    listax = (ListView) findViewById(R.id.listView2);

    listax.setAdapter(v);

    final TextView a = (TextView) findViewById(R.id.textView);



    listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


           switch (position) {
                case 0:
                    a.setText(R.string.wzor_newtona_wzor);
                    break;
                case 1:
                    a.setText(R.string.trzecie_prawo_keplera_wzor);
                    break;
                case 2:
                    a.setText(R.string.wzor_ciolkowskiego_wzor);
                    break;}

        }

    });
}

What I noticed is that when I open the second activity without the ListView everything works alright. So probably it is something about creating the ListView . But what? :/

Thank you for your help.

PS. I found an error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blogspot.mikeusz.physicsformulas/com.blogspot.mikeusz.physicsformulas.SecondActivityA}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

That NullPointerException should tell you that you are trying to assign an adapter to a non existing list. Make sure you have set the right id to the second list view and also make sure you set setContentView() in your second activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM