Mee-so Kim
Mee-so Kim

Reputation: 55

How to fix 'Unable to start activity... null object reference'

Please excuse me not good at using english...

I'm a beginner in android programming. I'm developing android alarm app, I want AlarmActivity to be displayed on the screen when AlarmReceiver is called. But when AlarmService.onCreate is called, an error occurs.

Process: com.example.app, PID: 11692 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.AlarmActivity}: android.view.InflateException: Binary XML file line #27: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

Here is my code.

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

context.startActivity(i);

From code above, Using debugger, I checked AlarmActivity.class and context is not null.

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

    ...
}

But, savedInstanceState in AlarmActivity.java is 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);
}
...
}

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>

I want to display AlarmActivity. But this error occurs. please somebody help me.

Upvotes: 0

Views: 582

Answers (1)

Mee-so Kim
Mee-so Kim

Reputation: 55

I found the answer!!!!!!!!! yeaaaaaaaaaaaaaaaaagghhhfbawjhefbwje!!!!!

I edited

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

to

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

It works!

Upvotes: 2

Related Questions