-3

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

jayeshsolanki93
  • 2,096
  • 1
  • 20
  • 37
Swarnveer
  • 490
  • 5
  • 23
  • 1
    The relevant part is `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)`, meaning that in your class `MainActivity`, you're trying to set a `onClickListener` on a Button that is null – NSimon May 31 '16 at 15:19
  • But I checked that's not true. – Swarnveer May 31 '16 at 15:21
  • 1
    Post some relevant code from your MainActivity – jayeshsolanki93 May 31 '16 at 15:22
  • 1
    Check your MainActivity, line 21 – NSimon May 31 '16 at 15:22
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vucko May 31 '16 at 15:27

2 Answers2

6

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();
NSimon
  • 5,212
  • 2
  • 22
  • 36
0

You calling setContentView method wrong place

 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);
            } 
        }); 
Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
  • Is there any mistake in Toast part of main ? Since i am getting toast message while running this app. – Swarnveer May 31 '16 at 15:33
  • try this one by one.... you comment and uncomment toast message..... @Override protected void onCreate(Bundle savedInstanceState) { // Toast.makeText(this,"onCreate = ",Toast.LENGTH_LONG).show(); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(this,"onCreate = ",Toast.LENGTH_LONG).show(); } – Muhammad Waleed May 31 '16 at 17:22