-1

hi im making an app which will show a snackbar for the user when he/she opens the app for the first time i instalized the coordinator layout and did every thing right but the app crashes on my phone and tells me that im attempting to invoke a virutal metod on null object refrences (sorry for my bad english i just have no time to fix my grammar and typos)

here is my code

public class MainActivity extends AppCompatActivity {

private CoordinatorLayout coordinatorLayout;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.activity_menu, menu);
    return super.onCreateOptionsMenu(menu);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);

    shows();


    DisplayMetrics metrics = new DisplayMetrics();
    MainActivity.this.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    float yInches = metrics.heightPixels / metrics.ydpi;
    float xInches = metrics.widthPixels / metrics.xdpi;
    double diagonalInches = Math.sqrt(xInches * xInches + yInches * yInches);
    if (diagonalInches >= 6.5) {
        setContentView(R.layout.activity_main_7inch);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    } else if (diagonalInches <= 4.0) {
        setContentView(R.layout.activity_main_small);
    } else {
        setContentView(R.layout.activity_main);
    }

}

public void shows() {
    Boolean isFirstRun = getSharedPreferences("PREFERENCES", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);
   if(isFirstRun) {
        showfirstTimesnackbar();
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("isFirstRun", false).commit();

    }
}

private void showfirstTimesnackbar() {
    Snackbar snackbar = Snackbar.make(coordinatorLayout, "Save your game first!", Snackbar.LENGTH_LONG);
            snackbar.setAction("MORE INFO", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Warning!")
                            .setMessage("Save your game before using any cheat! \n Typing in any cheat will disable achievements for your game.")
                            .setPositiveButton("OK", null)
                            .setIcon(R.drawable.ic_alert_warning);

                }
            });
            snackbar.show();
}
}

my logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SparidApps.gta_v_cheats/com.example.user.gtav_cheats.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at



com.example.user.gtav_cheats.MainActivity.showfirstTimesnackbar(MainActivity.java:75)
                                                                                 at com.example.user.gtav_cheats.MainActivity.onCreate(MainActivity.java:48)
Tarek Zoubi
  • 53
  • 1
  • 9

1 Answers1

1

You are accessing your view before setting layout to your Activity.

Try following

DisplayMetrics metrics = new DisplayMetrics();
MainActivity.this.getWindowManager().getDefaultDisplay().getMetrics(metrics);

float yInches = metrics.heightPixels / metrics.ydpi;
float xInches = metrics.widthPixels / metrics.xdpi;
double diagonalInches = Math.sqrt(xInches * xInches + yInches * yInches);
if (diagonalInches >= 6.5) {
    setContentView(R.layout.activity_main_7inch);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


} else if (diagonalInches <= 4.0) {
    setContentView(R.layout.activity_main_small);
} else {
    setContentView(R.layout.activity_main);
}

coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
shows();
Nikhil
  • 3,711
  • 8
  • 32
  • 43