Swarnveer Swarnveer - 6 months ago 23
Android Question

Application forced stopped. Why?

I am unable to decode the logcat report. I am learning android and trying to implement intent, toast and custom toast in single app.

As far I know I did it correctly. I am attaching logcat trace.

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) 


Main activity

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


Also tell me how to find the problem by using the log trace

Answer

You'll need to invert the order you're setting your buttons :

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

This is quite important, since you first need to call setContentView, because that view is then used to call view.findViewById. If you try to do it before setting the view, you will not be able to get any of the view's elements.

EDIT : As for your second question, replace :

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