0

so I'm trying to make an app..(or more specifically..practicing putting stuffs together to see how they work) which will set an alarm if I click a button...and at the set time..it will change the wallpaper of my phone. the app runs perfectly until the set time comes..n when the app is supposed to change my wallpaper..the app crashes..n throws a null pointer exception.

my MainActivity class code:

package com.example.wp;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.io.IOException;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    public Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar c = Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,14); //i want to change wallpaper at 2:00 . 
                c.set(Calendar.MINUTE,0);
                c.set(Calendar.SECOND,0);
                SetAlarm(c);

            }
        });
    }
    public void changewp(){
        Bitmap bit = BitmapFactory.decodeResource(getResources(),R.drawable.wp);
        WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());

        try {
            wm.setBitmap(bit);
            Toast.makeText(getApplicationContext(),"done",3).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(),"failed",3).show();
        }
    }
    public void SetAlarm(Calendar c){
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, alertrec.class);
        PendingIntent pen = PendingIntent.getBroadcast(this, 1,intent,0);
        am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pen);
    }

}

my alartrec class code:

package com.example.wp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class alertrec extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MainActivity m = new MainActivity();
        m.changewp(); 

    }
}

I have also tried to write the code inside changewp() method in the onReceive() method...bt wasn't able to use the parameters correctly. So if you have a better (but not too complex) way to accomplish what I am trying too..plz let me know that. And one more thing...I am not an expert of JAVA..so the mistake I'm making maybe stupid..but I have spend a lot of time finding a solution..and found nothing.

Any kind of help will be greatly appreciated.

N Kabir
  • 3
  • 2
  • what your `logcat` is saying? I think you can't create an instance of `MainActivity` in your broadcast receiver like that – ruben Nov 15 '20 at 07:40
  • In addition to @ruben comment, add the ```WallpaperManager``` class or parts of it. I think this class is giving you the NPE here ```WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext()); ``` – private static Nov 15 '20 at 07:59
  • @ruben ,I found the error : " Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference ". – N Kabir Nov 15 '20 at 10:50

1 Answers1

0

Or you can move the changewp()method in your BroadcastReceiver like this:

public class alertrec extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        changewp(context);
    }

    public void changewp(Context context){
        Bitmap bit = BitmapFactory.decodeResource( context.getResources(),R.drawable.ic_launcher_background);
        WallpaperManager wm = WallpaperManager.getInstance(context);

        try {
            wm.setBitmap(bit);
            Toast.makeText(context,"done",Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(context,"failed",Toast.LENGTH_LONG).show();
        }
    }
}
ruben
  • 1,745
  • 5
  • 25
  • 47
  • It worked..!!! But is there a way to do the same thing without moving the changewp() method in alertrec class??? – N Kabir Nov 15 '20 at 11:21
  • yes, check this : https://stackoverflow.com/questions/22241705/calling-a-activity-method-from-broadcastreceiver-in-android – ruben Nov 15 '20 at 12:22