0

I have these two same lines of code on each of my 5 activities, although this is the only one that get's the nullPointerException.

I have tried to check both the XML file and the JAVA file, which seem "clean", however I cannot seem the be able to fix this.

my Messages class :

public class Messages extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton Rd1, Rd2, Rd3, Rd4;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_messages);

    Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
    getSupportActionBar().setCustomView( R.layout.abs_layout_home );


    radioGroup = findViewById( R.id.radioGroup );
    Rd1 = findViewById( R.id.radioButton );
    Rd2 = findViewById( R.id.radioButton2 );
    Rd3 = findViewById( R.id.radioButton3 );
    Rd4 = findViewById( R.id.radioButton4 );
    radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (Rd1.isChecked()) {
                Intent intent = new Intent( getApplicationContext(), BottomActivity.class );
                startActivity( intent );
            }
            if (Rd2.isChecked()) {
                Intent intent1 = new Intent( getApplicationContext(), DashBoard.class );
                startActivity( intent1 );
            }
            if (Rd3.isChecked()) {
                Intent intent2 = new Intent( getApplicationContext(), SettingsActivity.class );
                startActivity( intent2 );
            } else {
                if (Rd4.isChecked()) {
                    Intent intent3 = new Intent( getApplicationContext(), Messages.class );
                    startActivity( intent3 );
                }


            }
        }
    } );


}
}

Here is the file that is allegedly null:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/AppName"
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:fontFamily="monospace"
    android:gravity="center"
    android:text="@string/Messages"
    android:textColor="#FFFFFF"
    android:textSize="23sp"
     />

 </LinearLayout>

Here is the stack trace:

  Caused by: java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203)
        at com.example.shrinkio.Messages.onCreate(Messages.java:22)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

It is expected to use a personalized action bar.

Arbaz Pirwani
  • 935
  • 7
  • 22
Tomas Mota
  • 672
  • 5
  • 27
  • You should post the full stacktrace, tell us which object is null, and where you thought it was getting a value. – takendarkk May 03 '19 at 19:29
  • 1
    Seems that your getSupportActionBar is returning null. Are you using a theme that has action bar? Take a look here: https://stackoverflow.com/questions/41741165/getsupportactionbar-returns-null-in-android-app – Ricardo A. May 03 '19 at 19:45

1 Answers1

1

The problem is getSupportActionBar(), it is returning null at this line:

Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );

Perhaps is because Messages extends AppCompatActivity which probably does not extend ActionBarActivity. Read this: getSupportActionBar() The method getSupportActionBar() is undefined for the type TaskActivity. Why?

user1039663
  • 1,230
  • 1
  • 9
  • 15