I want to edit textview text from MainActivity class and I put a function which is connected to interface, which then goes into another class named JobService (class for sending something with threads in it). But I am getting an error in constructor part It alludes to null pointer exception at myCallback part. Even when I give the string with code inside (timestampJob) it goes to null pointer exception.
//MainActivity class
public class MainActivity extends AppCompatActivity implements MyCallback
TextView timestampMails;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timestampMails = (TextView) findViewById(R.id.timeStampMainMails);
}
//function for changeing the textview connected with interface to JobService class
public void setTimestampMain(String time){
timestampMails.setText(time);
}
//Interface class
public interface MyCallback {
public void setTimestampMain(String myString);
}
//JobService class
public class JobService extends android.app.job.JobService {
//interface call
MyCallback myCallback = null;
//constructor
public JobService(MyCallback callback){
this.myCallback = callback;
}
public void timeStampJob(String time){
//HERE IS THE EXCEPTION (I think)
if(myCallback != null){
myCallback.setTimestampMain(time);
}
}
@Override
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "Job started");
doBackgroundWork(params);
return true;
}
private void doBackgroundWork(JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<1; i++){
//get device time function and populate function with that string
String deviceTime = new SimpleDateFormat("HH:mm").format(Calendar.getInstance().getTime());
timeStampJob(deviceTime);
if(jobCancelled){
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Log.d(TAG, "Job finished:");
jobFinished(params, false);
}
}).start();
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "Job cancelled before completion");
jobCancelled = true;
return true;
}
Process: com.example.gmailemailsolution, PID: 3944
java.lang.RuntimeException: Unable to instantiate service
com.example.gmailemailsolution.JobService: java.lang.InstantiationException:
java.lang.Class<com.example.gmailemailsolution.JobService> has no zero
argument constructor (HERE)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3268)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.InstantiationException:
java.lang.Class<com.example.gmailemailsolution.JobService> has no zero
argument constructor (HERE)
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3265)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Tried interface class, calling it with constructor and making textview static but did not made it work.