java.lang.NullPointerException上只有一项活动?

时间:2019-05-03 19:26:25

标签: java android nullpointerexception

我的5个活动中的每行都有这两行相同的代码,尽管这是唯一获得nullPointerException的代码。

我试图检查看起来“干净”的XML文件和JAVA文件,但是我似乎无法解决此问题。

我的消息类:

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


            }
        }
    } );


}
}

这是据称为空的文件:

<?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>

这是堆栈跟踪:

  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) 

希望使用个性化的操作栏。

1 个答案:

答案 0 :(得分:1)

问题是getSupportActionBar(),它在此行返回null:

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

也许是因为消息扩展了AppCompatActivity,而AppCompatActivity可能没有扩展ActionBarActivity。阅读:getSupportActionBar() The method getSupportActionBar() is undefined for the type TaskActivity. Why?