Saturday, 21 November 2015

XML : Java NullPointerException with listview

I've been designing a drawer navigation bar where you can click a function that should call out the "versions_layout.xml" with inside a listview. Though, after I made everything the app crashes. I even tried by doing a separate java file with a ListActivity class, that worked but it showed nothing and mostly important I couldn't click anymore my drawer opening button.

I'll attach the separate java file I made in case I have to re-put it, and my actual MainActivity as of now ( I followed a tutorial for bringing everything in it without making a separate java file )

MainActivity:

  public class MainActivity extends AppCompatActivity          implements NavigationView.OnNavigationItemSelectedListener {        String[] android_versions={              "IIDX1", "IIDX2", "IIDX3"};        ListView listView; //List View object declaration      ArrayAdapter<String> adapter;          NavigationView navigationView = null;      Toolbar toolbar = null;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            listView=(ListView)findViewById(R.id.id_list_view); //object declaration            adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android_versions);            listView.setAdapter(adapter); //connect the adapter to list view          listView.setOnItemClickListener(new AdapterView.OnItemClickListener()          {              @Override              public void onItemClick(AdapterView<?> parent, View view, int position, long id)              {              }ArrayAdapter<String> adapter;          });              //Set the fragment initially          Fragment fragment = new Fragment();          android.support.v4.app.FragmentTransaction fragmentTransaction =                  getSupportFragmentManager().beginTransaction();          fragmentTransaction.replace(R.id.fragment_container, fragment);          fragmentTransaction.commit();            toolbar = (Toolbar) findViewById(R.id.toolbar);          setSupportActionBar(toolbar);            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);          fab.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View view) {                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                          .setAction("Action", null).show();              }          });            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);          drawer.setDrawerListener(toggle);          toggle.syncState();            navigationView = (NavigationView) findViewById(R.id.nav_view);          navigationView.setNavigationItemSelectedListener(this);      }        @Override      public void onBackPressed() {          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);          if (drawer.isDrawerOpen(GravityCompat.START)) {              drawer.closeDrawer(GravityCompat.START);          } else {              super.onBackPressed();          }      }        @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.main, menu);          return true;      }        @Override      public boolean onOptionsItemSelected(MenuItem item) {          // Handle action bar item clicks here. The action bar will          // automatically handle clicks on the Home/Up button, so long          // as you specify a parent activity in AndroidManifest.xml.          int id = item.getItemId();            //noinspection SimplifiableIfStatement          if (id == R.id.action_settings) {              return true;          }            return super.onOptionsItemSelected(item);      }        @SuppressWarnings("StatementWithEmptyBody")      @Override      public boolean onNavigationItemSelected(MenuItem item) {          // Handle navigation view item clicks here.          int id = item.getItemId();            if (id == R.id.nav_versions) {              //Set the fragment initially              versions_Fragment fragment = new versions_Fragment();              android.support.v4.app.FragmentTransaction fragmentTransaction =                      getSupportFragmentManager().beginTransaction();              fragmentTransaction.replace(R.id.fragment_container, fragment);              fragmentTransaction.commit();          }          if (id == R.id.nav_mycard) {              //Set the fragment initially              mycard_Fragment fragment = new mycard_Fragment();              android.support.v4.app.FragmentTransaction fragmentTransaction =                      getSupportFragmentManager().beginTransaction();              fragmentTransaction.replace(R.id.fragment_container, fragment);              fragmentTransaction.commit();          } else if (id == R.id.nav_notes) {              //Set the fragment initially              notes_Fragment fragment = new notes_Fragment();              android.support.v4.app.FragmentTransaction fragmentTransaction =                      getSupportFragmentManager().beginTransaction();              fragmentTransaction.replace(R.id.fragment_container, fragment);              fragmentTransaction.commit();          } else if (id == R.id.nav_fav) {              //Set the fragment initially              fav_Fragment fragment = new fav_Fragment();              android.support.v4.app.FragmentTransaction fragmentTransaction =                      getSupportFragmentManager().beginTransaction();              fragmentTransaction.replace(R.id.fragment_container, fragment);              fragmentTransaction.commit();          } else if (id == R.id.nav_options) {              //Set the fragment initially              options_Fragment fragment = new options_Fragment();              android.support.v4.app.FragmentTransaction fragmentTransaction =                      getSupportFragmentManager().beginTransaction();              fragmentTransaction.replace(R.id.fragment_container, fragment);              fragmentTransaction.commit();          } else if (id == R.id.nav_about) {              //Set the fragment initially              about_Fragment fragment = new about_Fragment();              android.support.v4.app.FragmentTransaction fragmentTransaction =                      getSupportFragmentManager().beginTransaction();              fragmentTransaction.replace(R.id.fragment_container, fragment);              fragmentTransaction.commit();          }          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);          drawer.closeDrawer(GravityCompat.START);          return true;      }  }`    

Old "listview" java filed I had before linked to "versions_layout.xml" for the previous attempt:

  public class listview extends Activity {  private String[] style = {"IIDX 1st Style", "IIDX 2nd Style", "IIDX 3rd Style", "IIDX 4th Style"};    private ListView stylelistview;  private ArrayAdapter arrayAdapter;    //Called when the activity is created  @Override          protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.versions_layout);        stylelistview = (ListView) findViewById(R.id.stylelistview);        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, style);      stylelistview.setAdapter(arrayAdapter);  }      }    

verions_layout.xml

`

  <ListView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/id_list_view"      android:layout_alignParentTop="true"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true"      android:choiceMode="singleChoice"      android:layout_alignParentBottom="true" />     </RelativeLayout>    

Thanks for any help!

No comments:

Post a Comment