如何修复“无法启动活动...空对象引用”

时间:2019-11-05 11:29:24

标签: java android

请原谅我不擅长英语...

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

  

进程:com.example.app,PID:11692       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.app/com.example.app.AlarmActivity}:   android.view.InflateException:二进制XML文件第27行:尝试   调用虚拟方法'boolean   空对象引用上的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为空。

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

AlarmReceiver.java

...
public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("LOGGING", "AlarmReceiver");

        Intent sIntent = new Intent(context, AlarmService.class);
        sIntent.putExtra("state", intent.getStringExtra("state"));
        sIntent.putExtra("time", intent.getSerializableExtra("time"));
        sIntent.putExtra("id", intent.getStringExtra("id"));

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

        context.startActivity(i);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(sIntent);
        }
        else {
            context.startService(sIntent);
        }
    }
}
...

// 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。 但是会发生此错误。请有人帮我。

1 个答案:

答案 0 :(得分:2)

我找到了答案!!!!!!! yeaaaaaaaaaaaaaaaaaaagghhhfbawjhefbwje !!!!!

我编辑了

<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"/>

视图->视图

有效!