android.content.Context.getPackageName()' on a null object reference
I have found the mistake what I did.
We need to get the activity instance from the override method OnAttach()
For example,
public MainActivity activity;
@Override
public void onAttach(Activity activity){
this.activity = activity;
}
Then pass the activity as context as following.
Intent mIntent = new Intent(activity, MusicHome.class);
DialogFragment 'java.lang.String android.content.Context.getPackageName()' on a null object reference
Update
Store context when onAttach method called.
public class ExpensesLeisureDialog extends DialogFragment {
Context context;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
.
.
}
And use context variable when you have required.
java.lang.String android.content.Context.getPackageName()' on a null object reference
Your architecture is flawed. The IntentService
is created when you call startService()
. Then onCreate()
and onHandleIntent()
is called in the Service. Upon completion of onHandleIntent()
, if there are no other pending Intents to be processed the Service stops.
You are making this call in MainActivity
before you call startService()
:
getLRLD.received_MainActvity_Context(context);
You don't show how you are setting the reference variable getLRLD
, but this isn't going to work. The Service isn't actually created until you call startService()
.
You are trying to establish a communcations channel between the Service and the Activity. However, you are using IntentService
which is a non-persistent Service. It gets created as needed and when it has nothing to do it goes away. You therefore cannot pass a reference to the Service to the Activity, and you also should not pass a reference to the Activity to the Service.
The best way to achieve communication between the Service and the Activity is one of the following:
Service sends a broadcast
Intent
with the data. The Activity can register a BroadcastRecevier to listen for this.Activity creates a
PendingIntent
containing the necessary ACTION, COMPONENT, etc. and passes this to the Service as an "extra" in theIntent
it uses when callingstartService()
. The Service callssend()
on thatPendingIntent
in order to return data to the Activity.
In both cases the Service doesn't need to know anything about the Activity, and the Activity doesn't need a reference to the Service.
EDIT: Add code to start Activity
If you want to start an Activity, you can do this in the Service.onHandleIntent()
:
Intent intent1 = new Intent(this, Map.class);
intent1.putExtra("list_data", data);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent1);
Using Intent.FLAG_ACTIVITY_SINGLE_TOP
will prevent Android from launching a new instance of the Activity, if there is already an instance of the Activity running. In case the Activity is already running, you can get the "extras" from the Intent
by overriding onNewIntent()
in the Activity.
If you use this approach you don't need to use the broadcast Intent to send the data from the Service to the Activity.
how to fix go to next activity in adapter (Context.getPackageName()' on a null object reference)
you must initialize your context
before use it:
public NoteAdapter(List<Querynotes> querynotes, MainActivity mainActivity){
mContext = mainActivity; \\ add this line
this.querynotes=querynotes;
}
java.lang.String android.content.Context.getPackageName()' on a null object reference
try this
context=MainActivity.this
mAdapter = new MoviesAdapter(movieList,context) ;
android.content.Context.getPackageName()' on a null object reference
for me i was trying to use the application with no base context accidently.
so i had in my application override:
@Override
protected void attachBaseContext(Context newBase) {
}
//instead of calling its super:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);}
Related Topics
Recyclerview Inside Scrollview Not Scrolling Smoothly
How to Clear an Imageview in Android
Compile with Proguard Gives Simexception: "Local Variable Type Mismatch"
Get Free Space on Internal Memory
How to Set Font Custom Font to Spinner Text Programmatically
How to Make a View with Rounded Corners
Pick Any Kind of File via an Intent in Android
Mediaplayer Stutters at Start of Mp3 Playback
Fragmentcontainerview Using Findnavcontroller
How to Keep My Android Service Running When the Screen Is Turned Off
Error:Failed to Open Zip File. Gradle's Dependency Cache May Be Corrupt
Failed to Find Build Tools Revision 23.0.1
Android App Crashes When Launched in Debug Mode
Nullpointerexception When Trying to Access Views in a Kotlin Fragment
How to Create Socket Connection in Android
How to Dismiss a Progress Bar Even If There Is No View to Populate in the Firebaselistadapter