So I keep getting the same error no matter how many times I try to tweak my code here and there. I will be posting every little bit of code that has to do with the error.
I will not include unnecessary code so if my code doesn't look complete it's because I cut it out to make it easier for people to diagnose the issue!
First Class
I wrote !!! error !!! after the code that was said to have been the cause of the error according to my log.
SharedPreferences sf;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
sf = getSharedPreferences("MyData", MODE_PRIVATE);
checkCookie();
}
//function for making sure there's data
public void checkCookie() {
editor = sf.edit();
if (sf.getInt("cookies", -1) == -1) {
editor.putInt("cookies", 0);
}
}
//function for retrieving amount of cookies
public int getCookies()
{
editor = sf.edit(); !!! Error here !!!
int x = sf.getInt("cookies", 0);
return x;
}
//function for changing amount of cookies
public void changeCookies(int x)
{
editor = sf.edit();
editor.putInt("cookies", x);
}
Second Class
I put !!! error !!! at the second error given which basically is what calls the function with the error in it.
//MyActivity is the first class
MyActivity mCookie = new MyActivity();
//function for adding cookies
public void cookieCalc(){
if (Math.abs((5-(millis/1000))) <= 0.005)
mCookie.changeCookies(100 * CB); ... much more similar code
//an if statement within a function which just calls the function getcookies and cookiecalc
if (loop == 2){
cookieCalc();
myText.setText("= " + mCookie.getCookies()); !!! this calls the function which gives error !!!
}
The error is 100% because of SharedPreferences as it worked with normal variables, but I needed to save the variables and was told this is a very easy method to do so. If all of this is still not enough and I cannot explain it to you I will then paste my entire code on gitHub or another viewing platform.
Error Logs
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at app.z0nen.slidemenu.MyActivity.changeCookies(MyActivity.java:74)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at app.z0nen.slidemenu.menu1_Fragment.cookieCalc(menu1_Fragment.java:75)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at app.z0nen.slidemenu.menu1_Fragment$1.onClick(menu1_Fragment.java:148)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.view.View.performClick(View.java:5242)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.widget.TextView.performClick(TextView.java:10571)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21196)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6938)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
You should never create activity instance by yourself! Only system should do that.
If you create instance yourself, onCreate will never get called, as this instance won't go through standard activity lifecycle.
do not new instance by yourself. onCreate was br
Solved
SharedPreferences sf = this.getActivity().getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
because it was in a fragment class I had to add this.getActivity() and I also ended up adding Activity.MODE_PRIVATE. I probably was getting null pointer exception because it had no way to access my Activity class because fragments extend something else? Just a guess of mine, so far it is still not working how I want it to but I prevented crashes. edit it wasn't working because I didn't do editor.commit(); to save the changes.
Related
Currently I have already intialized
myDb = new DatabaseHelper(this);
and have no problem using DatabaseHelper's methods in my Main class. But now I'm trying to initialize another class
expenses = new AddExpenses();
and when I try to set the context for that it goes red and when I try to use AddExpenses class' method in my Main class, my app crashes.
The logcat when the app crashes :-
--------- beginning of crash
10-26 00:44:16.144 4105-4105/project.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: project.myapplication, PID: 4105
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor project.myapplication.DatabaseHelper.getLatestAmount(java.lang.String, java.lang.String)' on a null object reference
at project.myapplication.AddExpenses.recalculateAllSpendings(AddExpenses.java:273)
at project.myapplication.SpendingTracker$5.onClick(SpendingTracker.java:188)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
That's the Code to get the AndroidID:
String userID = Settings.Secure.getString(getApplicationContext()
.getContentResolver(), Settings.Secure.ANDROID_ID);
When i start the Application i get the following logcat review:
04-28 01:30:02.012 9328-9328/com.hubermoritz.beercounter.beercounter02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hubermoritz.beercounter.beercounter02, PID: 9328
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hubermoritz.beercounter.beercounter02/com.hubermoritz.beercounter.beercounter02.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
04
Tried with:
public String userID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
also, didn't work either. By the way I'm well aware off that it's not a perfect way to recognize users, because the number can change due to factory reset or changing phone and few other ways.
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
You are attempting to call a method that you are inheriting from Activity from a field initializer. Do not do this. In general, do not call inherited methods on Activity until after super.onCreate() has been called in your onCreate() method. You are welcome to have a field, but do not initialize it until after super.onCreate().
You can't use getContentResolver until after the constructor is completed. Set this variable in your onCreate instead and you should be ok
After I upload my apk to testfairy and install it I get a java.lang.VerifyError crash
The class extends android.support.v4.app.Fragment
This is the function that is crashing. It crashes on the function execution, not the click but when I remove the onClick. Inner stuff all works.
fitroom_lly is LinearLayout
mixpanel defined :
MixpanelAPI mixpanel = MixpanelAPI.getInstance(getActivity(), getResources().getString(R.string.Mix_Panel_Token));
Function:
private void AddOnFittingRoomClickListener() {
fitroom_lly.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mixpanel.track("Fitting room");
}
});
}
Error:
java.lang.VerifyError: co/slider/fashion/Slider/SliderFragment$6
at co.slider.fashion.Slider.SliderFragment.AddOnFittingRoomClickListener(SliderFragment.java:334)
at co.slider.fashion.Slider.SliderFragment.onCreateView(SliderFragment.java:135)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
I work for TestFairy. I need a bit information, is your app multidex'd or a single dex? That's the only VerifyError we ever encountered, and we have an answer for that. Though I would need you to email support at testfairy.com, and one of the technical developer will explain to you the workaround. We're making this solution available to everybody, but in the meanwhile, only support is explaining this.
My attempt to simply/clearly explain a requirement to play music, show mp3 detail like time elapsed/remaining, and scrubbing with seek bar.
Activity hosts Fragment. Service has MediaPlayer.
Fragment simultaneously starts service and displays Dialog.
Dialog buttons control MediaPlayer. I can pause/play using av controls from the dialog.
NullPointerException occurs when I try to set dialog views with info. Seems the problem is that dialog doesn't attach quick enough for me to update with media player details and set seek bar progression.
I wasn't initially using a service but that approach didn't satisfy other requirements, like seamless audio on rotation and playing audio even if the dialog gets closed by the user.
Project source available on GitHub.
Update #1. Add stacktrace. Crash occurs at PreviewDialog.java:112. I commented this line in the linked repo with a note to uncomment in order to observe NPE.
0-07 14:35:09.475 12202-12202/com.es0329.workshop E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
10-07 14:35:09.495 12202-12202/com.es0329.workshop E/MediaPlayer: Should have subtitle controller already set
10-07 14:35:09.500 12202-12202/com.es0329.workshop E/MediaPlayer: Should have subtitle controller already set
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: FATAL EXCEPTION: main
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: Process: com.es0329.workshop, PID: 12202
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at com.es0329.workshop.PreviewDialog.updateUi(PreviewDialog.java:112)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at com.es0329.workshop.PreviewDialog.onEvent(PreviewDialog.java:97)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at com.es0329.workshop.EventBus.publish(EventBus.java:40)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at com.es0329.workshop.MusicService$2.onPrepared(MusicService.java:50)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2548)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5289)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
10-07 14:35:09.502 12202-12202/com.es0329.workshop E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Update #2. Post code snippet. When the MediaPlayer is ready, it notifies the dialog. If the dialog receives a message of "READY" it calls the #updateUi below. From what I can tell, the service begins and the mediaplayer data is ready/available BEFORE my dialog (which contains that timeRemaining textView) is attached.
private void updateUi(MediaPlayer player) {
int timeRemainingValue = player.getDuration();
timeRemaining.setText(String.valueOf(timeRemainingValue)); // NPE!
}
Add a try catch statement around the setting of the time remaining to handle to Null pointer exception.
General comment: I used created a new thread in my service which would update the UI information (i.e. duration text and SeekBar).
I have an activity which displays a DialogFragment, inside that dialog fragment I have a linearlayout which contains one button, and a fragmentLAyout where I want to display the info.
I´d have to display one list of elements, and if any of them are pressed I´ll have to display the details for them. I thought the best way to do that is with fragments, when trying to call
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.framePlaceHolder, listAccountFragment).commit();
after creating my listAccountFragment, I´m getting this error
10-01 10:53:34.004 12836-12836/com.bbva.compassBuzz.qa.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bbva.compassBuzz.qa.debug, PID: 12836
java.lang.IllegalStateException: Fragment does not have a view
at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1746)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:943)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.Fragment.performResume(Fragment.java:1836)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:993)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
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:1384)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1179)