Daniel O Mensah Daniel O Mensah - 4 months ago 54
Java Question

How to call non-static method from another class Android Studio

So I am trying to call a method once an item is tapped in Java Android studio.

This is where the method I want to call is

getFavourites();


public class Favourites extends Activity {
public void getFavourites(){
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(0);
setTitle("Favourites");
}
}


Here is where I want to call the method
getFavourtites();


public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_favourite) {
Favourites getFav = new Favourites();
getFav.getFavourites();
} else if (id == R.id.nav_search) {
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(1);
setTitle("Search");
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_sendFeedback) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}


I don't really know what I am doing wrong but every time I run it, the app just crash.

EDIT: here is the full stack for favourites

package com.ctt.mrdan.croydontramtime2;

import android.app.Activity;
import android.widget.ViewFlipper;

/**
* Created by MrDan on 16/07/2016.
*/
public class Favourites extends Activity {
public void getFavourites(){
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(0);
setTitle("Favourites");
}
}


Here is the full stack for the Main

package com.ctt.mrdan.croydontramtime2;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar 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 = (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_favourite) {
Favourites getFav = new Favourites();
getFav.getFavourites();
} else if (id == R.id.nav_search) {
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(1);
setTitle("Search");
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_sendFeedback) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

Answer

What i see without a Logcat output:

You're instantiating Favourites and then calling getFavourites() on the new instance. getFavourites() calls findViewById(), which implicitly calls getWindow().findViewById(), but the Activity has no content view, resulting in a NullPointerException.

Favourites has no purpose at all.

You could just move the code from getFavourites() to onNavigationItemSelected():

if (id == R.id.nav_favourite) {
    ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
    viewFlipper.setDisplayedChild(0);
    setTitle("Favourites");
} else if (id == R.id.nav_search) {
    ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
    viewFlipper.setDisplayedChild(1);
    setTitle("Search");
}

Also, you shouldn't initialize your ViewFlipper instance on every item click.

Declare it as an instance variable and initialize it in onCreate().