无法开始活动

时间:2016-09-08 16:11:43

标签: java android nullpointerexception android-context

我正在尝试在我的应用程序中显示一个快餐栏但应用程序崩溃并给我nullpointer excpetion并且它说我试图在空指针异常上调用虚拟方法 这是我的代码

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);

    Boolean isFirstRun = getSharedPreferences("PREFERENCES", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);
    if(isFirstRun){
        Snackbar.make(, "Save your game first!", Snackbar.LENGTH_LONG)
                .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);

                    }
                })
                .show();
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("isFirstRun", false).commit();
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setSupportActionBar(toolbar);
    }




    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);
    }
}

logcat的

  

com.Example.example E / AndroidRuntime:FATAL EXCEPTION:main                                                                                    处理:com.Example.example,PID:17354                                                                                    java.lang.RuntimeException:无法启动活动   ComponentInfo {com.SparidApps.gta_v_cheats / com.example.user.example.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法   'android.content.Context android.view.ViewGroup.getContext()'上   null对象引用                                                                                        在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)                                                                                        在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)                                                                                        在android.app.ActivityThread.access $ 900(ActivityThread.java:157)                                                                                        在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1356)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                        在android.os.Looper.loop(Looper.java:148)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5530)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)                                                                                     引起:java.lang.NullPointerException:尝试调用虚拟   方法'android.content.Context android.view.ViewGroup.getContext()'   在null对象引用上                                                                                        在android.support.design.widget.Snackbar。(Snackbar.java:180)                                                                                        在android.support.design.widget.Snackbar.make(Snackbar.java:209)                                                                                        在   com.example.user.gtav_cheats.MainActivity.onCreate(MainActivity.java:48)                                                                                        在android.app.Activity.performCreate(Activity.java:6272)                                                                                        在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)                                                                                        在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)                                                                                        在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)                                                                                        在android.app.ActivityThread.access $ 900(ActivityThread.java:157)                                                                                        在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1356)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                        在android.os.Looper.loop(Looper.java:148)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5530)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)

0 个答案:

没有答案