申请被迫停止。为什么?

时间:2016-05-31 15:17:12

标签: android android-studio

我无法解码logcat报告。我正在学习android并尝试在单个应用程序中实现intent,toast和custom toast。

据我所知,我做得正确。我正在附加logcat跟踪。

FATAL EXCEPTION: main
Process: com.bt4u.customtoast, PID: 24490
Theme: themes:{default=overlay:com.rr.neptune, iconPack:system, fontPkg:com.rr.neptune, com.android.systemui=overlay:com.rr.neptune, com.android.systemui.navbar=overlay:com.rr.neptune}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bt4u.customtoast/com.bt4u.customtoast.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.bt4u.customtoast.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
at android.app.ActivityThread.access$900(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5466) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

主要活动

 b1 = (Button)findViewById(R.id.button1);
    b2 = (Button)findViewById(R.id.button2);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(MainActivity.this,"Just clicked",Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.BOTTOM|Gravity.RIGHT,0,0);
        }
    });

    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this,second.class);
            startActivity(i);
        }
    });
   }

还告诉我如何使用日志跟踪找到问题

2 个答案:

答案 0 :(得分:6)

您需要反转您设置按钮的顺序:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);

这非常重要,因为您首先需要调用setContentView,因为该视图会用于调用view.findViewById。如果您在设置视图之前尝试执行此操作,则无法获取任何视图元素。

编辑:至于你的第二个问题,请替换:

Toast toast = Toast.makeText(MainActivity.this,"Just clicked",Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM|Gravity.RIGHT,0,0);

with:

Toast.makeText(MainActivity.this,"Just clicked",Toast.LENGTH_SHORT).setGravity(Gravity.BOTTOM|Gravity.RIGHT,0,0).show();

答案 1 :(得分:0)

你调用setContentView方法错误的地方

 super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);
    b1 = (Button)findViewById(R.id.button1); 
        b2 = (Button)findViewById(R.id.button2); 



        b1.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this,"Just clicked",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM|Gravity.RIGHT,0,0);
            } 
        }); 

        b2.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,second.class);
                startActivity(i);
            } 
        });