Coordinatorlayout 为空
Coordinatorlayout is null
嗨,我正在制作一个应用程序,当 he/she 第一次打开该应用程序时,它会为用户显示一个小吃店我安装了协调器布局并正确地完成了所有操作,但该应用程序在我的 [=20] 上崩溃了=] 并告诉我我正试图在空对象引用上调用虚拟方法(抱歉我的英语不好,我只是没有时间修正我的语法和拼写错误)
这是我的代码
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();
}
}
我的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)
在将 layout
设置为 Activity
之前,您正在访问 view
。
尝试关注
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();
嗨,我正在制作一个应用程序,当 he/she 第一次打开该应用程序时,它会为用户显示一个小吃店我安装了协调器布局并正确地完成了所有操作,但该应用程序在我的 [=20] 上崩溃了=] 并告诉我我正试图在空对象引用上调用虚拟方法(抱歉我的英语不好,我只是没有时间修正我的语法和拼写错误)
这是我的代码
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();
}
}
我的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)
在将 layout
设置为 Activity
之前,您正在访问 view
。
尝试关注
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();