I need to replace a Fragment in one Activity with another Fragment, below is the layer file of the Activity:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/home_layout_container">
<fragment android:name="com.foo.FragA"
android:id="#+id/home_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
so by default there is FragA, now I want to replace it with FragB in activity, I did:
public void onRegionClicked(Region region) {
RegionInfoFragment rif = RegionInfoFragment.newInstance(region);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.home_list, rif);
}
But I got exception:
>6:24:40.685: ERROR/AndroidRuntime(9194): Uncaught handler: thread main exiting due to uncaught exception
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addViewInner(ViewGroup.java:1857)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addView(ViewGroup.java:1752)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addView(ViewGroup.java:1709)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addView(ViewGroup.java:1689)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:578)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1219)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:380)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.os.Handler.handleCallback(Handler.java:587)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.os.Looper.loop(Looper.java:123)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at java.lang.reflect.Method.invoke(Method.java:521)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at dalvik.system.NativeStart.main(Native Method)
What can I do?
Thanks!
You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.
Try to commit() at the end of the ft, you need to commit() your changes to let it know.
getFragmentManger()
.beginTransaction
.replace(R.id.frame,Yourfragment.newInstance(),null)
.addtobackstack
.commit();
People always say I am one-liner, so here is one-liner solution for you
Related
I am using **HttpClient 4.4.1*. When I create a CloseableHttpClient object, its thrown verifyError. How can I fix it?
Here is the line which thrown error:
CloseableHttpClient client = HttpClients.createDefault();
Error message:
05-06 18:22:10.911: E/AndroidRuntime(9641): FATAL EXCEPTION: AsyncTask #1
05-06 18:22:10.911: E/AndroidRuntime(9641): java.lang.RuntimeException: An error occured while executing doInBackground()
05-06 18:22:10.911: E/AndroidRuntime(9641): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-06 18:22:10.911: E/AndroidRuntime(9641): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-06 18:22:10.911: E/AndroidRuntime(9641): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-06 18:22:10.911: E/AndroidRuntime(9641): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-06 18:22:10.911: E/AndroidRuntime(9641): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-06 18:22:10.911: E/AndroidRuntime(9641): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-06 18:22:10.911: E/AndroidRuntime(9641): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-06 18:22:10.911: E/AndroidRuntime(9641): at java.lang.Thread.run(Thread.java:841)
05-06 18:22:10.911: E/AndroidRuntime(9641): Caused by: java.lang.VerifyError: org/apache/http/conn/ssl/DefaultHostnameVerifier
05-06 18:22:10.911: E/AndroidRuntime(9641): at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:901)
05-06 18:22:10.911: E/AndroidRuntime(9641): at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
05-06 18:22:10.911: E/AndroidRuntime(9641): at com.srl.mbill.MeterLogin$LoginTask.doInBackground(MeterLogin.java:141)
05-06 18:22:10.911: E/AndroidRuntime(9641): at com.srl.mbill.MeterLogin$LoginTask.doInBackground(MeterLogin.java:1)
I am restarting the activity after updating some fields through api call, after 10-15 continuous updates it is giving an exception as below :
05-06 17:12:55.412: E/SurfaceFlinger(115): GraphicBufferAlloc::createGraphicBuffer(w=1232, h=800) failed (Out of memory), handle=0x0
05-06 17:12:55.412: E/BufferQueue(115): [com.ht.coremoney/com.ht.coremoney.cards.TransactionFullDetailActivity] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
05-06 17:12:55.420: E/libEGL(1350): eglMakeCurrent:534 error 3003 (EGL_BAD_ALLOC)
05-06 17:12:55.428: E/ViewRootImpl(1350): OutOfResourcesException initializing HW surface
05-06 17:12:55.428: E/ViewRootImpl(1350): android.view.Surface$OutOfResourcesException: eglMakeCurrent failed EGL_BAD_ALLOC
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.HardwareRenderer$GlRenderer.createEglSurface(HardwareRenderer.java:920)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:748)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1621)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.Choreographer.doFrame(Choreographer.java:525)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.os.Handler.handleCallback(Handler.java:615)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.os.Looper.loop(Looper.java:137)
05-06 17:12:55.428: E/ViewRootImpl(1350): at android.app.ActivityThread.main(ActivityThread.java:4895)
05-06 17:12:55.428: E/ViewRootImpl(1350): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 17:12:55.428: E/ViewRootImpl(1350): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 17:12:55.428: E/ViewRootImpl(1350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-06 17:12:55.428: E/ViewRootImpl(1350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-06 17:12:55.428: E/ViewRootImpl(1350): at dalvik.system.NativeStart.main(Native Method)
05-06 17:12:55.428: E/android.os.Debug(1350): !#Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_surfaceoom
Kindly suggest me the solution.
Let's analyse what this says:
GraphicBufferAlloc::createGraphicBuffer(w=1232, h=800) failed (Out of memory)
You seem to allocate a native buffer, 985600 pixels. If they use a 32-bit color format, that's 3.94 MB of memory. Taking into account that you do this 10 to 15 times, you allocate up to 59.14 MB of memory, far too much for most devices.
My guess is that you forgot to actually free these buffers, therefore wasting lots of memory.
When using native code, your memory is only freed when your process is killed, not when the activity is restarted. Therefore, your code allocates a new buffer every time the activity is started. You should either free when the activity closes or reuse the already allocated buffer.
Let's understand Exception and Error in java.
In your situation you got an error by doing something wrong you can't handle or manage It like exception,
That's mean you need to analyze you memory management of your Application..
By the using the eclipse Memory Analyzer tools.
It's plugged in already with Android ADT.
Here are link for using memory
Analyzer.
http://www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html
http://android-developers.blogspot.in/2011/03/memory-analysis-for-android.html?m=1
I know it's not your direct solution but with the help of this tool you can analyze your all memory leakage and try to reuse your preallocated buffer ,
And android is also recommend that's do not use very complex layouting in a or heavy nested layouting in single layout, instead of all this you can break up all complex layout into sm fragments.
Hope this information help you.
I am using iText library for generate new pdf in my app. but my question is how can i use not embedded fonts in program? I'll try this code but its not working for me.
FONTS = "assets/Zapfino.ttf"
BaseFont bf = BaseFont.createFont(FONTS,"CP1251", BaseFont.EMBEDDED);
font = new Font(bf,18);
Paragraph pr = new Paragraph(p12,font);
document.add(pr);
and giv error this
05-06 10:08:31.740: W/System.err(1129): com.itextpdf.text.DocumentException: assets/Zapfino.ttf cannot be embedded due to licensing restrictions.
05-06 10:08:31.740: W/System.err(1129): at com.itextpdf.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:371)
05-06 10:08:31.740: W/System.err(1129): at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:699)
05-06 10:08:31.740: W/System.err(1129): at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:615)
05-06 10:08:31.740: W/System.err(1129): at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:450)
05-06 10:08:31.740: W/System.err(1129): at com.example.pdfformate.MainActivity.createPdf(MainActivity.java:108)
05-06 10:08:31.740: W/System.err(1129): at com.example.pdfformate.MainActivity.access$0(MainActivity.java:90)
05-06 10:08:31.750: W/System.err(1129): at com.example.pdfformate.MainActivity$1.onClick(MainActivity.java:74)
05-06 10:08:31.750: W/System.err(1129): at android.view.View.performClick(View.java:2485)
05-06 10:08:31.750: W/System.err(1129): at android.view.View$PerformClick.run(View.java:9080)
05-06 10:08:31.750: W/System.err(1129): at android.os.Handler.handleCallback(Handler.java:587)
05-06 10:08:31.750: W/System.err(1129): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 10:08:31.750: W/System.err(1129): at android.os.Looper.loop(Looper.java:123)
05-06 10:08:31.750: W/System.err(1129): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-06 10:08:31.750: W/System.err(1129): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:08:31.750: W/System.err(1129): at java.lang.reflect.Method.invoke(Method.java:507)
05-06 10:08:31.750: W/System.err(1129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-06 10:08:31.750: W/System.err(1129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-06 10:08:31.750: W/System.err(1129): at dalvik.system.NativeStart.main(Native Method)
Any help?
I am trying to show a adview in my android application, but it shows an InflateException. This terminates the app and I cannot do anything.
Here is my logcat -
05-06 14:09:09.428: E/AndroidRuntime(928): FATAL EXCEPTION: main
05-06 14:09:09.428: E/AndroidRuntime(928): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kodspider.parkon/com.kodspider.parkon.HomeActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class com.google.ads.AdView
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.os.Looper.loop(Looper.java:137)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-06 14:09:09.428: E/AndroidRuntime(928): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 14:09:09.428: E/AndroidRuntime(928): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 14:09:09.428: E/AndroidRuntime(928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 14:09:09.428: E/AndroidRuntime(928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 14:09:09.428: E/AndroidRuntime(928): at dalvik.system.NativeStart.main(Native Method)
05-06 14:09:09.428: E/AndroidRuntime(928): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.google.ads.AdView
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-06 14:09:09.428: E/AndroidRuntime(928): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.Activity.setContentView(Activity.java:1881)
05-06 14:09:09.428: E/AndroidRuntime(928): at com.kodspider.parkon.HomeActivity.onCreate(HomeActivity.java:31)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.Activity.performCreate(Activity.java:5104)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-06 14:09:09.428: E/AndroidRuntime(928): ... 11 more
05-06 14:09:09.428: E/AndroidRuntime(928): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /system/framework/com.google.android.maps.jar:/data/app/com.kodspider.parkon-2.apk
05-06 14:09:09.428: E/AndroidRuntime(928): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-06 14:09:09.428: E/AndroidRuntime(928): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-06 14:09:09.428: E/AndroidRuntime(928): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
05-06 14:09:09.428: E/AndroidRuntime(928): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
05-06 14:09:09.428: E/AndroidRuntime(928): ... 22 more
You need to put the admob jar into the libs folder of your project.
The same error appears to me and i solved with few simple steps.
You need to remove the appcombat dependency from your project.
For removing right click your project->android->remove app combat and make sure you have selected android 4.2.2
And then import the google admob jar into your project.
I am trying to generate a dynamic table problematically It's working properly above API > 11 but its showing some error like this
05-06 12:35:30.303: E/AndroidRuntime(386): FATAL EXCEPTION: main
05-06 12:35:30.303: E/AndroidRuntime(386): java.lang.NoClassDefFoundError: android.app.ActionBar$LayoutParams
05-06 12:35:30.303: E/AndroidRuntime(386): at com.ica.icacounselor.Follow_ups_list_third_level.populateTable(Follow_ups_list_third_level.java:105)
05-06 12:35:30.303: E/AndroidRuntime(386): at com.ica.icacounselor.Follow_ups_list_third_level.onCreate(Follow_ups_list_third_level.java:65)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.os.Looper.loop(Looper.java:123)
05-06 12:35:30.303: E/AndroidRuntime(386): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-06 12:35:30.303: E/AndroidRuntime(386): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 12:35:30.303: E/AndroidRuntime(386): at java.lang.reflect.Method.invoke(Method.java:507)
05-06 12:35:30.303: E/AndroidRuntime(386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-06 12:35:30.303: E/AndroidRuntime(386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-06 12:35:30.303: E/AndroidRuntime(386): at dalvik.system.NativeStart.main(Native Method)
My code follows
//--------------------------------------------------------------------------------------------------
// Creating Table Header
//--------------------------------------------------------------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
final TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
the ActionBar is presented in API 11. You can't use it in lower API levels.
Yes you can't use ActionBar below API level 11.
To use actionbar in lower api levels use ActionBarSherlock
I had imported import android.app.ActionBar.LayoutParams;
I should import android.widget.TableRow.LayoutParams;