I have googled here and there for two days already, but no solution seems not be working. Issue is within Fitness.getSessionsClient(this, Objects.requireNonNull(GoogleSignIn.getLastSignedInAccount(2ndclass.mContext)))
and its passed activity ( this
) and context ( 2ndClass.mContext
) variables, which I can populate (variables or not null or empty via debugger inspection) by many found approaches already. Still - all the time I get either NPE (java.lang.NullPointerException) or that getApplicationContext() is null. Any ideas?
Crash log line:
E/<SOME TAG>: Error during PUT connection... Data: {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":13.11,"pressure":1033,"humidity":77,"temp_min":10,"temp_max":16.11},"visibility":10000,"wind":{"speed":3.1,"deg":330},"clouds":{"all":69},"dt":1568357213,"sys":{"type":1,"id":1414,"message":0.0113,"country":"GB","sunrise":1568352700,"sunset":1568398909},"timezone":3600,"id":2643743,"name":"London","cod":200} Message: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
Code pieces:
1st (MainActivity) class:
public class 1stClass extends AppCompatActivity {
public static 1stClass instance;
public static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
mContext = getApplicationContext();
}
public final void 1stmethod(){
2ndClass.2ndmethod();
}
/**
* @return instance
*/
@Contract(pure = true)
public static 1stClass getInstance() { return instance; } // return.getInstance();
public static Context getContext() {
// return instance.getApplicationContext();
return mContext;
}
}
2nd class:
public class 2ndClass extends 1stClass{
static 2ndClass instance;
public final void 2ndmethod() {
instance = this;
3rdClass MFP = new 3rdClass();
MFP.3rdmethod(..);
}
/**
* @return instance
*/
@Contract(pure = true)
public static 2ndClass getInstance() { return instance; } //return.getInstance();
}
Non-activity (3rd) class:
public final class 3rdClass extends 2ndClass {
static 3rdClass instance;
public void 3rdmethod() {
instance = this;
Fitness.getSessionsClient(this, Objects.requireNonNull(GoogleSignIn.getLastSignedInAccount(2ndclass.mContext))) // <---- ERROR HERE
.insertSession(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Session insert was successful!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.wtf(TAG, "There was a problem inserting the session: " +
e.getLocalizedMessage());
}
});
/**
* @return instance
*/
@Contract(pure = true)
public static MFPalInterface getInstance() {
return instance;
} // return.getInstance();
}
I think I understand what you're doing, even though the code is one great mess.
You are trying to create your own activity in your 2ndClass
's method '2ndmethod()' and then call the method on that newly created 3rdClass
.
public final void 2ndmethod() {
3rdClass MFP = new 3rdClass();
MFP.3rdmethod(..);
}
Even though you've extended 3rdClass
from your AppCompatActivity
, you've not asked the system to create the Activity instance and attempted to create it yourself. This means the newly created instance has no context and the system does not know about it.
What You Need to Do
Start the activity by calling the startActivity()
method of your current running Activity
(in this case 2ndClass I think) and by passing an intent. Like so:
Intent intent = new Intent(this, 3rdClass.class); // this is your current Activity's reference.
currentActivity.startActivity(intent);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.