This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am trying to implement a GoogleMap object into my code. When I use getMapASync() and assign the map created in the onMapReady() call back to the GoogleMap object I want to use, it works within that method, but once I refer to it outside of that method it says that it is still null. Why?
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Map not connected", Toast.LENGTH_SHORT).show();
}
}
else{
setContentView(R.layout.activity_main);
}
String mapType2 = Integer.toString(mMap.getMapType());
Toast.makeText(this, mapType2 + " mMap", Toast.LENGTH_SHORT).show();
//This is for debugging purposes
}
public boolean servicesOK(){
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(isAvailable == ConnectionResult.SUCCESS){
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,this,ERROR_DIALOG_REQUEST);
dialog.show();
}
else{
Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap(){
if(mMap == null){
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
return (mMap != null);
}
#Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
}
}
The error message I get is this:
FATAL EXCEPTION: main
Process: com.ramaya947yahoo.mymaps, PID: 1037
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ramaya947yahoo.mymaps/com.ramaya947yahoo.mymaps.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
at com.ramaya947yahoo.mymaps.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
Its because you are ivoking
String mapType2 = Integer.toString(mMap.getMapType());
in onCreate method when map is not yet initializer nor assigned.
All map initialization related code should be placed in
#Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
//here
}
As this is called some time after activity is already created.
Related
I have a second class from which I call some methods to handle app updating. There is a progress dialog in them so I have to pass the current application context to its constructor. The code works fine when called directly in my MainActivity onCreate(), but it breaks down when I delegate the code to an external class. What's going wrong?
Method Call in OnCreate():
private AppUpdateHelper appUpdateHelper = new AppUpdateHelper(getApplicationContext());
#Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper.handleAppUpdate();
}
Delegate Class:
public class AppUpdateHelper {
private Context mContext;
public AppUpdateHelper(Context mContext) {
this.mContext = mContext;
}
public void handleAppUpdate() {
String versionCode = getVersionCode(); // Get app's current version code
// Is app update to date?
if (isAppCurrent(versionCode)) {
promptAppUpdate();
}
}
private String getVersionCode() {
String versionCode = null;
try {
PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
versionCode = pInfo.versionName;
// Log.w(mContext.getClass().getSimpleName(), "Current Version: " + versionCode);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
private boolean isAppCurrent(String versionCode) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
String userAppVersion = installation.getString("appVersion");
return !userAppVersion.equals(versionCode);
}
private void promptAppUpdate() {
SweetAlertDialog pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE);
pDialog.setTitleText("Update Available!");
pDialog.setContentText("You must update to continue using Yeet Club!");
pDialog.setConfirmClickListener(sDialog -> {
final String appPackageName = mContext.getPackageName(); // getPackageName() from Context or Activity object
try {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (ActivityNotFoundException anfe) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
sDialog.dismissWithAnimation();
});
pDialog.setCancelable(false);
pDialog.showCancelButton(false);
pDialog.show();
}
}
Exception:
08 - 20 15: 43: 43.874 21456 - 21456 / com.app.android E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.android, PID: 21456
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {
com.app.android / com.app.android.activity.MainActivity
}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2458)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access$900(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()'
on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java: 117)
at com.app.android.activity.MainActivity. < init > (MainActivity.java: 77)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java: 1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2448)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access$900(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
This is very simple.
You can get context instance after called OnCreate
private AppUpdateHelper appUpdateHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper = new AppUpdateHelper(getApplicationContext());
appUpdateHelper.handleAppUpdate();
}
You can pass the activity context it self using this key word.
#Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper = new AppUpdateHelper(this);
appUpdateHelper.handleAppUpdate();
}
You can't call getApplicationContext() before the class initialized
You can only call after the class initialized. You can get it on onCreate() or other methods called by android.
you are safe to call inside onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper = new AppUpdateHelper(getActivity()); // if it is Fragment
appUpdateHelper = new AppUpdateHelper(this); // if it is Activity
appUpdateHelper.handleAppUpdate();
}
Application context won't work with Dialog. Because application context does not have theme related information. Use Activity context instead of Application Context
So I'm working on an app and I had this part working for days, and out of no where it just stopped working for no reason...
I also had the same error when I was trying to use another headless fragment in my MainActivity, but ended up replacing the fragment with inner methods inside of the MainActivity and everything went back to working properly.
However, I can't rewrite every bit of code I have just to avoid using fragments. The fragment code is below.
public class IMEIFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback{
public static final String TAG_IMEI = "IMEILoader";
protected Activity mActivity;
private String RecordedIMEI;
//public static final String CHECK_INTERNET = "network_connection";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return null; //Do we need this at all?
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity = context instanceof Activity ? (Activity) context : null;
mActivity = activity;
}
//Is this needed?
#SuppressWarnings("deprecation")
#Override
public void onAttach(Activity activity) {
activity = getActivity();
if (isAdded() && activity != null) {
super.onAttach(activity);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
mActivity = activity;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onDetach() {
super.onDetach();
mActivity = null;
}
public String loadIMEI(Context context) {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions(context);
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions(context);
}
if (RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions(context);
}
if (RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return RecordedIMEI;
}
private void requestPermissions(Context context) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.i("loadIMEIService", "READ_PHONE_STATE permission not granted, asking for it...");
// TODO create proper notification content
PermissionHelper.requestPermissions(((PriceActivity) getActivity()),
new String[]{Manifest.permission.READ_PHONE_STATE},
Constants.PERM_REQUEST_PHONE_STATE,
getString(R.string.notify_perm_title),
getString(R.string.notify_perm_body),
R.drawable.ic_security);
}
}
// Callback received when a permissions request has been completed.
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
boolean isGranted = false;
for (int i = 0; i < grantResults.length; i++)
if (permissions[i].equals(Manifest.permission.READ_PHONE_STATE) && (grantResults[i] == PackageManager.PERMISSION_GRANTED))
isGranted = true;
if (isGranted) {
Context context = getActivity().getApplicationContext();
permissionGrantedActions(context);
}
else
Log.w("loadIMEIService", "READ_PHONE_STATE permission not granted. loadIMEI will not be available.");
}
public String permissionGrantedActions(Context context) {
//Have an object of TelephonyManager
TelephonyManager tm =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//Get IMEI Number of Phone
String IMEINumber = tm.getDeviceId();
if(IMEINumber != null) {
Log.i("loadIMEIService", "IMEI number recorded!");
}
return IMEINumber;
}
}
Error is below:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.project1, PID: 5498
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.project1/com.android.project1.main.MainActivity}: java.lang.IllegalStateException: Fragment IMEIFragment{3e80da7 IMEILoader} not attached to Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Fragment IMEIFragment{3e80da7 IMEILoader} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:805)
at android.app.Fragment.getString(Fragment.java:827)
at com.android.project1.fragments.IMEIFragment.requestPermissions(IMEIFragment.java:107)
at com.android.project1.fragments.IMEIFragment.loadIMEI(IMEIFragment.java:80)
at com.android.project1.main.MainActivity.onCreate(MainActivity.java:108)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
And here's the relevant part of my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDeviceCode = (TextView) findViewById(R.id.device_code);
// Initializing headless fragment
mFragment =
(IMEIFragment) getFragmentManager()
.findFragmentByTag("IMEILoader");
if (mFragment == null) {
mFragment = new IMEIFragment();
getFragmentManager().beginTransaction()
.add(mFragment, "IMEILoader").commit();
}
if (mFragment != null) {
mNumber = mFragment.loadIMEI(MainActivity.this);
mDeviceCode.setText(Html.fromHtml("<b>IMEI</b>: " + mNumber));
}
I literally had the exact same code working for over a week. Anyone knows what could be the problem?
Edit 1: The error is pointing to requestPermissions inside my fragment
Fragments should be self contained as much as possible. You are calling directly into your IMEIFragment from the activity,
Caused by: java.lang.IllegalStateException: Fragment IMEIFragment{3e80da7 IMEILoader} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:805)
at android.app.Fragment.getString(Fragment.java:827)
at com.android.project1.fragments.IMEIFragment.requestPermissions(IMEIFragment.java:107)
at com.android.project1.fragments.IMEIFragment.loadIMEI(IMEIFragment.java:80)
at com.android.project1.main.MainActivity.onCreate(MainActivity.java:108)
You can't do that. Adding the fragment via a transaction from the activity is an asynchronous operation. E.g., when the commit() method completes, the fragment is not initialized. Moreover, you have no way of knowing when it's initialized. That's why it should be self contained. The fragment decides when to call loadIMEI(), not the activity.
If you really need it to be initiated by the activity, you can add a callback from the fragment to the activity like,
void onFragmentReady(Fragment f);
Or something.
And yes, onCreateView() should return something. If your fragment really doesn't have any UI at all, you don't need it to be a fragment.
I have a main Activity and a headless Fragment.
The headless Fragment is supposed to get the IMEI number of the phone to be recorded and returned to the main Activity.
I had this bug for a few hours now and I can't seem to shake it off.
Here's the logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidproject.example, PID: 5418
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidproject.example/com.androidproject.example.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
at com.androidproject.example.HeadlessFragment.loadIMEI(HeadlessFragment.java:110)
at com.androidproject.example.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
And here's the relevant part of the code in the MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDeviceCode = (TextView)findViewById(R.id.device_code);
// Initializing headless fragment
mFragment =
(HeadlessFragment) getFragmentManager()
.findFragmentByTag("IMEILoader");
if(mFragment == null) {
mFragment = new HeadlessFragment();
getFragmentManager().beginTransaction()
.add(mFragment, "IMEILoader").commit();
}
if(mFragment != null){
mNumber = mFragment.loadIMEI(); //Here's the error
mDeviceCode.setText(Html.fromHtml("<b>IMEI</b>: " + mFragment.loadIMEI()));
}
And here's the HeadlessFragment code:
//Called when the 'loadIMEI' function is triggered.
public String loadIMEI() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions();
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions();
}
if(RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return RecordedIMEI;
}
public String permissionGrantedActions() {
//Get IMEI Number of Phone
TelephonyManager tm =(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
String IMEINumber = tm.getDeviceId();
//RecordedIMEI = IMEINumber;
if(IMEINumber != null) {
Log.i("IMEI Loader", "IMEI number recorded!");
}
return IMEINumber;
}
}
I tried different things but no luck. I think getActivity().getApplicationContext() is pointing no where, which means this is being called before headless fragment is attached to mainactivity?
I've been stuck on this for quite some time and need some help.
I think you're going about this "headless Fragment" idea wrong since...
A Fragment represents a behavior or a portion of user interface in an Activity.
You seem to want a static utility class that holds a Context and can call some permission things. Take away the extends Fragment from the code and other answer(s), and this is basically what you are left with.
Turn it into a singleton, and you don't need the Context parameter everywhere.
(code untested)
May not completely work. For example, not sure how the ActivityCompat.OnRequestPermissionsResultCallback works...
But it exposes the functionality you need without working around a Fragment lifecycle.
public final class IMEILoader {
public static IMEILoader mInstance;
private Context mContext;
private IMEILoader() {}
private IMEILoader(Context c) {
this.mContext = c;
}
// Singleton pattern
public static IMEILoader getInstance(Context c) {
if (!(c instanceof ActivityCompat.OnRequestPermissionsResultCallback)) {
throw new Exception("Passed context not implementing permission callbacks");
}
if (mInstance == null) mInstance = new IMEILoader(c);
return mInstance;
}
public String load() {
String recordedIMEI = null;
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions();
} else {
// READ_PHONE_STATE permission is already been granted.
recordedIMEI = permissionGrantedActions();
}
if(recordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return recordedIMEI;
}
public String permissionGrantedActions() {
return null;
}
}
And you can use that in the Activity like
class FooActivity extends AppCompatActivity impelements ActivityCompat.OnRequestPermissionsResultCallback {
private IMEILoader loader;
...
public void onCreate(Bundle b) {
...
loader = IMEILoader.getInstance(MainActivity.this);
String blah = loader.load();
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
...
}
Change This Line
mNumber = mFragment.loadIMEI();
To
mNumber = mFragment.loadIMEI(MainActivity.this);
And your function would be like this.
public String loadIMEI(Context context) {
//Context context = getActivity().getApplicationContext();
//Activity activity = context instanceof Activity ? (Activity) context : null;
//mActivity = activity;
// Check if the READ_PHONE_STATE permission is already available.
//this.context = getActivity().getApplicationContext();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions();
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions();
}
if(RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return RecordedIMEI;
}
I want to get value from another class, but when I get it in second class it is null. I think it is caused bcs when is something added to this String that code does not run.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
a lot of not important code.......................................
FirebaseDatabase database5 = FirebaseDatabase.getInstance();
DatabaseReference myRef5 = database5.getReference("userdata");
myRef5.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
lot of not improtant code.............................................
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
a lot of not important code...........................................
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
progressBar.setVisibility(View.GONE);
textViewPleaseWait.setVisibility(View.INVISIBLE);
recipientEmail = marker.getTitle();
Log.i("omg", recipientEmail);
dialogBuilder.setMessage("Username: " + usernameAlert + "\n" + "Gender: " + genderAlert
+ "\n" + "Age: " + ageAlert + "\n" + marker.getSnippet() + "\n" + marker.getTitle());
dialogBuilder.setPositiveButton("Send message", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(getApplicationContext(), SendMessage.class);
startActivity(intent);
recipientEmail = marker.getTitle();
}
});
dialogBuilder.setNegativeButton("Close", null);
dialogBuilder.show();
}
}.start();
}
And here it is.... I set value to recipientEmail when timer is done but I must run timer by clicking on marker on map. I can't set value recipientEmail outside of this timer bcs value wouldn't be same. So when I call it in another class, normally:
MapsActivity mapsActivity = new MapsActivity();
recipientUsername2 = mapsActivity.getRecipientEmail();
Log.i("values2", mapsActivity.recipientEmail);
And after running second activity app crashes.
08-28 21:34:25.943 4609-4609/com.samo.facedatefb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samo.facedatefb, PID: 4609
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samo.facedatefb/com.samo.facedatefb.SendMessage}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.i(Log.java:160)
at com.samo.facedatefb.SendMessage.onCreate(SendMessage.java:79)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Could you help me please?
Here you create a new, (presumably empty) MapsActivity instance:
MapsActivity mapsActivity = new MapsActivity();
And two lines below, you try to read from it:
recipientUsername2 = mapsActivity.getRecipientEmail();
No wonder it is empty. This MapsActivity is not the MapsActivity you're looking for... (Just this time it is not a Jedi trick.)
Try to grasp it this way: When you set the recipientEmail field in MapsActivity, you write it on a piece of paper. With the new MapsActivity() constructor call, you get a new piece of paper from your drawer - a different one, and now you try to read something off that paper, which you wrote on the other one...
To be able to have that information, you have to put it somewhere - set it to an instance of a class that you pass around, set it to a field in the class you plan to use next, etc...
This time however, I think setting the recipientEmail String as an Extra to the SendMail activity would be the way to go:
Intent intent = new Intent(getApplicationContext(), SendMessage.class);
intent.putExtra("RECIPIENT_EMAIL", marker.getTitle());
startActivity(intent); //this has to be after the putExtra call
And when you try to access it in the SendMail activity:
String recipientEmail = null;
Bundle extras = getIntent().getExtras();
if(extras == null) {
throw new IllegalStateException("No email address found at SendMail activity!");
} else {
recipientEmail = extras.getString("RECIPIENT_EMAIL");
}
Also, read the answers to this question I usually turn to when having to deal with Extras.
Another thing: the recommended way to access object members is to set them private vland add getter methods and where applicable, setter methods. Just leaving the variables accessible is problematic.
Hi first of all i searched some similar questions like mine but unfortunately i couldn't find the similarity of my code to them so please here me out
I have a Main Activity Class
public class MainActivity extends AppCompatActivity {
public ProgressDialog loading;
public String[] itemer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RetrofitHandler handler = new RetrofitHandler();
handler.getContacts(MainActivity.this);
}
public void getter(String[] response, int size) {
String[] itemer;
itemer = new String[size];
if (response != null) {
itemer = response;
Toast.makeText(MainActivity.this, itemer[0], Toast.LENGTH_SHORT).show();
}
}
And a Handler for my result
public class RetrofitHandler {
public String[] item;
public static final String ROOT_URL = "http://api.androidhive.info";
public List<Contacts> contacts;
// final MainActivity main = new MainActivity();
public void getContacts(final Context context) {
final ProgressDialog loading = ProgressDialog.show(context, "Fetching Data", "Please wait...", false, false);
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
ContactsAPI api = adapter.create(ContactsAPI.class);
api.getContacts(new Callback<Contacts>() {
#Override
public void success(Contacts contacts, Response response) {
loading.dismiss();
MainActivity update = new MainActivity();
List<Contact> contactList = contacts.getContacts();
item = new String[contactList.size()];
int size = contactList.size();
for (int i = 0; i < contactList.size(); i++) {
item[i] = contactList.get(i).getName();
}
update.getter(item, size);
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(context, "Error Occured", Toast.LENGTH_LONG).show();
}
});
}
But I get an error on my response in the main activity here is my log
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.widget.Toast.<init>(Toast.java:102)
at android.widget.Toast.makeText(Toast.java:259)
at com.exist.kelvs.retrofit2.MainActivity.getter(MainActivity.java:55)
at com.exist.kelvs.retrofit2.RetrofitHandler$1.success(RetrofitHandler.java:41)
at com.exist.kelvs.retrofit2.RetrofitHandler$1.success(RetrofitHandler.java:28)
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I maked sure that the handler item where not null tested it with toast but when i pass it to getter it gives me the error where did i do wrong? :(
MainActivity update = new MainActivity();
Never instantiate activities with new. They are not initialized to be useful.
Instead, you can pass your activity as a reference where needed. Change
public void getContacts(final Context context)
to e.g.
public void getContacts(final MainActivity mainActivity)
and use mainActivity where you need an activity Context (such as with Dialogs) and when you need to invoke a method on MainActivity.
Note that generally passing activity references to async operations can be prone to significant memory leaks, and you need to take activity lifecycle into account - the activity might not be active when the async operation finishes.
try to delete the toast in your main activity or replace Mainactivity.this to getApplicationContext() .
from :
Toast.makeText(MainActivity.this, itemer[0], Toast.LENGTH_SHORT).show();
to :
Toast.makeText(getApplicationContext(), itemer[0], Toast.LENGTH_SHORT).show();