Balaji Kumar
Balaji Kumar

Reputation: 1

i Want to add the the action bar using getSupportActionBar() but it throws an Null pointer exception

Theese are the code package com.example.lee;

public class Activity_Cricket extends AppCompatActivity {

Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EdgeToEdge.enable(this);
    setContentView(R.layout.activity_cricket);



    button =findViewById(R.id.a_apply);
    String Activity = "Cricket";
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Activity_Cricket.this, Contact_form.class);
            intent.putExtra("activity_name", Activity);
            startActivity(intent);
        }
    });

}

} It does not show error in gradle build but when i try to open the activity, the app keeps on stopping and the logcat shows this error

enter code here

FATAL EXCEPTION: main Process: com.example.lee, PID: 20944 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lee/com.example.lee.Activity_Cricket}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) at com.example.lee.Activity_Cricket.onCreate(Activity_Cricket.java:30) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  2024

Upvotes: -1

Views: 41

Answers (2)

Damir Mustafin
Damir Mustafin

Reputation: 13

I guess you try to call .GetSupportActionBar() inside the Fragment. So the trouble is in the backwards compatibility. You'll need to cast it in the ActionBarActivity

((AppCompatActivity)getActivity()).getSupportActionBar().setSubtitle(R.string.subtitle);

The NullPointerException throws cause this command is in Activity API

Upvotes: 0

Priyanka patel
Priyanka patel

Reputation: 308

Have you put your activity in menifest file like this

<activity android:name="com.example.lee.Activity_Cricket" />

OR other option

check in your java file. have you put below line in onCreate() method

setContentView(R.layout.activity_main) //your layout file name

Upvotes: 0

Related Questions