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)
Related
My app is a RSS adapter who works with SQlite, viewpagers and RecyclerViews. Everything works fine but once I leave my app pressing home button, my app crashes and shows the following logcat:
FATAL EXCEPTION: main
Process: com.herprogramacin.hermosaprogramacion, PID: 14900
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.herprogramacin.hermosaprogramacion.UI.Adapters.RVAdapter)
at android.os.Parcel.writeSerializable(Parcel.java:1468)
at android.os.Parcel.writeValue(Parcel.java:1416)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
at android.os.Parcel.writeTypedArray(Parcel.java:1254)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384)
at android.os.Parcel.writeParcelable(Parcel.java:1437)
at android.os.Parcel.writeValue(Parcel.java:1343)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3701)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4622)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.io.NotSerializableException: android.database.sqlite.SQLiteCursor
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at android.os.Parcel.writeSerializable(Parcel.java:1463)
at android.os.Parcel.writeValue(Parcel.java:1416)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
at android.os.Parcel.writeTypedArray(Parcel.java:1254)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384)
at android.os.Parcel.writeParcelable(Parcel.java:1437)
at android.os.Parcel.writeValue(Parcel.java:1343)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3701)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4622)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Where we can read "Caused by: java.io.NotSerializableException:android.database.sqlite.SQLiteCursor"
Why Is it happening this?
EDIT 1:
How my app works.
Main activity gets the XML code for RSS and once it finishes, MainActivity get the cursor and generates the Recycler View Adapter.
Main activity creates the viewpager and sends to him the RecyclerView Adapter.
ViewPager creates a new fragment and passes the adapter to the new Fragment
The new fragment generates the view and sets the adapter with the data that ViewPager sent to him.
I realised that is crazy to generate the adapter in MainActivity and pass it to the fragment through 3 clases. Could be this the problem?
It sounds like you are holding a reference to a SQLiteCursor in an activity or other view logic. When Android is attempting to pause your activity it tries to serialize the data the activity currently has (and dependent objects) so that they can be deserialized when the activity resumes. Look around for how you are using SQLiteCursor, you should not be passing them around to your activity or any model objects used by your activity.
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
what`s wrong with this because I cant compile to apk. Error is Cannot use this in a static context but if I change this to context app freeze when dialog must appear.
public static void mydialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
}
Logcat error:
android.view.WindowLeaked: Activity com.test.testapp.activities.MenuActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{430fc9a8 V.E..... R.....I. 0,0-1048,466} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:457)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:288)
at com.dialog.dialog.showDialog(RateThisApp.java:229)
at com.dialog.dialog.showDialogIfNeeded(RateThisApp.java:108)
at com.test.testapp.activities.MenuActivity.onCreate(MenuActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
A static method is not associated with any particular instance of a class. It is a method associated with the class itself. Thus this has no meaning in a static method- there is no instance of the class to reference.
If this method is in a class that is a Context (e.g. an Application subclass or an Activity), then you can remove the static modifier and use this like you are already trying to do.
When running my application in Android Studio I get a NullPointerException at line 30 which is this:
llTheory.setOnClickListener(this);
which has always worked for me.. I am implementing View.OnClickListener and have the override method here:
#Override
public void onClick(View v) {
....
}
My LinearLayout is declared before the setOnClickListener here:
LinearLayout llTheory = (LinearLayout) findViewById(R.id.llTheory);
so what is going wrong? Here is my LogCat output:
10-20 21:09:24.045 14625-14625/com.timmo.applauncher E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.timmo.applauncher, PID: 14625
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timmo.applauncher/com.timmo.timmoapplauncher.Main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5153)
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:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.timmo.timmoapplauncher.Main.Init(Main.java:30)
at com.timmo.timmoapplauncher.Main.onCreate(Main.java:78)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5153)
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:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Fragments work a little bit differently.
You have to use the concept of rootview when dealing with them. Otherwise you will find yourself always coming across the dreaded "NULL POINTER" exception.
Here is how to go about it.
Define the rootview when you inflate each view for each fragment, then call the onClick functionality.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootview = inflater.inflate(R.layout.xml_you_want_to_inflate, container,false);
// Now call rootview and the onclick fuction, presumably for a list or button
rootview.findViewById(R.id.your_button/list_id_here).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff.
}
});
Another alternative is using getView which always returns the rootview. Try this instead
LinearLayout llTheory = (LinearLayout) getView().findViewById(R.id.llTheory);
This link explains a similar problem.
The former way however is the correct way as you isolate your fragments from each other and understand the view hierarchy correctly. I would read this. Trust me the time investment reading that stuff is worthwhile then the hours you'll spend otherwise with half-wit hack job ideas to make stuff work.
findViewById returns a null value when the requested view cannot be found.
Check in your corresponding xml that a view is indeed declared with the id #id/llTheory
A standard Java best practice would be checking a casted item before using it.
Do you have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_R_id_llTheory);
...
in your activity? Your activity (or fragment hosted by activity) must have layout with R.id.llTheory.
Overview:
I have a fragment-based app with a single activity. On launch, I add my main fragment and all works well. When I tap on something, I call replace() to swap out to the new fragment and add the transaction to the back stack.
Problem:
When the app dies in the background (or most easily repro'd with 'Don't Keep Activities'), and I relaunch the app from the home screen, I get the exception below.
Scenario:
1. Open app
2. Navigate to a secondary fragment (this problem doesn't occur if you never leave the main fragment)
3. Background the app
4. Let the app die naturally (~24hr), or enable 'Don't Keep Activites' in the Developer Options
5. Open app from home screen
Expected:
App opens to main fragment (regardless of what fragment was displayed when backgrounded)
Actual:
Crash below
Other Requirements:
- Background app and immediately tap from home screen must return to previous fragment (assuming it wasn't destroyed in the background).
Stack Trace (note absolutely no calls from my app itself, so nowhere for me to debug):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rcg.socialstitch/com.rcg.socialstitch.ViewStitchActivity}: java.lang.IllegalStateException: Fragment already added: ViewStitchFragment{42800520 #1 id=0x7f040030}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalStateException: Fragment already added: ViewStitchFragment{42800520 #1 id=0x7f040030}
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1133)
at android.app.BackStackRecord.run(BackStackRecord.java:618)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(NativeStart.java)
I believe I have solved this. My .replace() calls all added to the back stack, but my initial .add() did not. When I ensured my initial fragment addition was no the back stack, this no longer seems to happen.