Java 如何修复';无法启动活动。。。空对象引用';

Java 如何修复';无法启动活动。。。空对象引用';,java,android,Java,Android,请原谅我不善于使用英语 我是android编程的初学者。 我正在开发android alarm应用程序,我希望在调用AlarmReceiver时在屏幕上显示AlarmActivity。 但调用AlarmService.onCreate时,会发生错误 进程:com.example.app,PID:11692 java.lang.RuntimeException:无法启动活动组件信息{com.example.app/com.example.app.AlarmActivity}: android.vi

请原谅我不善于使用英语

我是android编程的初学者。 我正在开发android alarm应用程序,我希望在调用AlarmReceiver时在屏幕上显示AlarmActivity。 但调用AlarmService.onCreate时,会发生错误

进程:com.example.app,PID:11692 java.lang.RuntimeException:无法启动活动组件信息{com.example.app/com.example.app.AlarmActivity}: android.view.InflateException:二进制XML文件行#27:尝试 调用虚拟方法的布尔值 null对象引用上的java.lang.String.equals(java.lang.Object)“”

这是我的密码

Intent i = new Intent(context, AlarmActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(i);
从上面的代码中,我使用调试器检查了AlarmActivity.class和context是否为非空

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm);

    ...
}
但是,AlarmActivity.java中的savedInstanceState为null

MainActivity.java

public class MainActivity extends AppCompatActivity implements ServiceCallbacks {
...
public void setAlarm (String[] times) {
        int timesLen = times.length;

        for (int i = 0 ; i < timesLen ; i++) {
            if (times[i] != null) {
                "2019-11-05 20:24:00";
                int year = Integer.parseInt(times[i].substring(0, 4));
                int month = Integer.parseInt(times[i].substring(5, 7)) - 1;
                int date = Integer.parseInt(times[i].substring(8, 10));
                int hour = Integer.parseInt(times[i].substring(11, 13));
                int minute = Integer.parseInt(times[i].substring(14, 16));
                int second = Integer.parseInt(times[i].substring(17, 19));

                Calendar mCalendar = Calendar.getInstance();

                mCalendar.set(Calendar.YEAR, year);
                mCalendar.set(Calendar.MONTH, month);
                mCalendar.set(Calendar.DATE, date);
                mCalendar.set(Calendar.HOUR_OF_DAY, hour);
                mCalendar.set(Calendar.MINUTE, minute);
                mCalendar.set(Calendar.SECOND, second);

                intent = new Intent(MainActivity.this, AlarmReceiver.class);
                intent.putExtra("state", "on");
                intent.putExtra("time", mCalendar);
                intent.putExtra("id", i);

                pendingIntent = PendingIntent.getBroadcast(MainActivity.this, i, intent, 0);

                alarmManager.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);

                Log.d("LOGGING", "Alarm added: " + times[i]);
            }
        }

        Toast.makeText(MainActivity.this, "Alarm added.", Toast.LENGTH_SHORT);
}
...
}
//activity_alarm.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AlarmActivity"
    android:background="#CC000000">
        <RelativeLayout
            android:id="@+id/alarm-main-box"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:background="@drawable/layout_bg"
            android:layout_centerInParent="true">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="60dp"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="76.5dp"
                    android:text="nolvadex"
                    android:textSize="20dp"
                    android:textColor="#707070"
                    android:gravity="center"/>
                <view
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@drawable/textlines"
                    android:layout_marginRight="15.5dp"
                    android:layout_marginLeft="15.5dp"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="76.5dp"
                    android:text="femara"
                    android:textSize="20dp"
                    android:textColor="#707070"
                    android:gravity="center"/>
            </LinearLayout>
            <RelativeLayout
                android:layout_width="64dp"
                android:layout_height="90dp"
                android:background="@drawable/layout_bg_gray_circle"
                android:paddingBottom="10dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true">
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="300dp"
                android:layout_height="60dp"
                android:layout_alignParentTop="true"
                android:background="@drawable/layout_bg_pink">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="AM 8:00"
                    android:textSize="25dp"
                    android:textColor="#ffffff"
                    android:textAlignment="center"
                    android:gravity="center"
                    android:lineHeight="60dp" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="300dp"
                android:layout_height="72dp"
                android:layout_alignParentBottom="true"
                android:background="@drawable/layout_bg_gray">
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="90dp"
                android:layout_alignParentBottom="true"
                android:id="@+id/button-area">
                <RelativeLayout
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true">
                    <ImageButton
                        android:id="@+id/center_button"
                        android:layout_width="48dp"
                        android:layout_height="48dp"
                        android:background="@drawable/layout_round_center_button"
                        android:gravity="center_vertical|center_horizontal"
                        android:layout_marginTop="10dp"
                        android:layout_centerHorizontal="true"
                        android:elevation="2dp"
                        android:translationZ="2dp"
                        android:stateListAnimator="@null"
                        android:textColor="#fff"
                        android:src="@drawable/button_check"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="all"
                        android:textSize="12dp"
                        android:textColor="#F48999"
                        android:layout_marginTop="11dp"
                        android:gravity="center"
                        android:layout_below="@+id/center_button"/>
                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="83dp"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true">
                    <ImageButton
                        android:id="@+id/left_button"
                        android:layout_width="37dp"
                        android:layout_height="37dp"
                        android:gravity="center_vertical|center_horizontal"
                        android:background="@drawable/layout_round_left_button"
                        android:layout_marginTop="30dp"
                        android:layout_marginLeft="33dp"
                        android:textColor="#fff"
                        android:src="@drawable/button_x"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="next"
                        android:textSize="12dp"
                        android:layout_marginLeft="28dp"
                        android:layout_marginTop="2dp"
                        android:layout_below="@+id/left_button"/>
                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="83dp"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true">
                    <ImageButton
                        android:id="@+id/right_button"
                        android:layout_width="37dp"
                        android:layout_height="37dp"
                        android:gravity="center_vertical|center_horizontal"
                        android:background="@drawable/layout_round_right_button"
                        android:layout_marginTop="30dp"
                        android:layout_marginLeft="13dp"
                        android:textColor="#fff"
                        android:src="@drawable/button_clock"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="re"
                        android:textSize="12dp"
                        android:layout_marginLeft="9dp"
                        android:layout_marginTop="2dp"
                        android:layout_below="@+id/right_button"/>
                </RelativeLayout>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".AlarmActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="false" />

        <service
            android:name=".AlarmService"
            android:enabled="true" />
    </application>

</manifest>

我想显示AlarmActivity。
但这种错误发生了。请有人帮帮我。

我找到了答案!!!!!!!!! 是的,是的,是的

我编辑

<view
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/textlines"
    android:layout_marginRight="15.5dp"
    android:layout_marginLeft="15.5dp"/>


查看->查看


它起作用了

我找到了答案!!!!!!!!! 是的,是的,是的

我编辑

<view
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/textlines"
    android:layout_marginRight="15.5dp"
    android:layout_marginLeft="15.5dp"/>


查看->查看

它起作用了

很高兴听到很高兴听到
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/textlines"
    android:layout_marginRight="15.5dp"
    android:layout_marginLeft="15.5dp"/>