Adding Fragment to TabHost within another Fragment - java

I am trying to add a fragment as one of the tabs in my tabhost on android. I think I need to load the fragment using fragment manager, but I am unsure about how to approach this. Here is my code for setting up the tabHost:
Intent intent = new Intent(getActivity(), FilesFragment.class);
tabSpec = tabHost.newTabSpec("Page 1").setContent(intent).setIndicator("How to Print");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("Page 2").setContent(homePageTab2.getId()).setIndicator("Files");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("Page 3").setContent(homePageTab3.getId()).setIndicator("Drives");
tabHost.addTab(tabSpec);
This is the run time error:
Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost.addTab(TabHost.java:240)
at io.uprint.uprint.Home.onCreateView(Home.java:86)

The answer is right there in the logcat...
Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
From the docs for TabHost...
public void setup (LocalActivityManager activityGroup)
If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.
Parameters
activityGroup Used to launch activities for tab content.

Related

In android Studio, I want when i click on button , next activity/fragment should come from right side

In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}

Onclick method not found in parent or ancestor context

I get an error for a very simple thing. I have created a test button and an onClick method to just change the layout. I did it in a simple way so you could understand my problem better.
This is my button method:
public void accountButton (View v){
setContentView(R.layout.activity_start);
}
And this is the xml file of my button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="#+id/button"
android:layout_below="#+id/welcomeTxt"
android:layout_centerHorizontal="true"
android:layout_marginTop="149dp"
android:onClick="accountButton"
android:nestedScrollingEnabled="true" />
I get the following error when i click the button:
java.lang.IllegalStateException: Could not find method
accountButton(View) in a parent or ancestor Context for
android:onClick attribute defined on view class
android.support.v7.widget.AppCompatButton with id 'button'
You may be calling your setContentView method from another activity. This method inflates the layout.
If it's called outside the activity the layout belongs to, the button won't be found. The most common way to initilize an activity from another is by intent.
Intent intent = new Intent(this, StartActivity.class);
startActivity(intent);
And then, inside your activity you call setContentView(R.layout.your_activity_layout) to inflate the layout. If you do this, you can call the onClick(View view) referred in the xml file from inside your activity normally.
For those that still have issues, in Android Studio or Intellij IDE, this is likely to a cached version of the class you are using (or its predecessors), that is not being recompiled.
Do an "Invalidate Cache/restart" and your problems are likely to go away.
When I had this issue occur repeatedly, I noticed that it was due to a "File Lock", triggered by Google Drive, that was making a copy of my File(s). Temporary disabling that solved all my issues.
you should not call setContentView(R.layout.activity_start); inside onClick() mentod of button.you have to call setContentView(R.layout.activity_start); inside oncreate() method.
follow the steps to achieve it.
implement OnClickListener in your class
initialize the button in onCreate()
Button button = (Button) findViewById(R.id.button);
set the setOnClickListener() method for button
btn.setOnClickListener()
then call the method outside of oncreate()
public void accountButton (View v){ //do some thing which you want }

Android Tab Menu not working

I'm trying to make a MenuTab with android but I have a lot of problems. Here's my code
tabHost = (TabHost) A.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1 = tabHost.newTabSpec("tab_news");
spec1.setIndicator(
"", //Load news titlte
A.getResources().getDrawable(R.drawable.icon_menu_news) //Load icon
);
spec1.setContent(R.id.tab_news);
tabHost.addTab(spec1);
First question, why if I put "title" inside indicator I don't see the image?
Well now I want to create a new activity when this tab is selected.
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
firstTabSpec.setIndicator("First Tab Name").setContent(new Intent(A,Test.class));
This example doesn't work... I get this error
09-15 23:19:26.861: E/AndroidRuntime(14938): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.workactivity/com.workactivity.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
I try to google about setup but I get no matchs...
I follow this tutorial: http://www.androidpeople.com/android-tabhost-tutorial-part-1
And this one: http://android-pro.blogspot.com.es/2010/08/tabbed-applications-in-android.html
Thanks for all :)
Look at this, sounds like just the same problem solution.
While not seeing your code maybe you need change Activity base class from Activity to ActivityGroup

Android replace fragment doesn't hide underlying fragment

I have the following problem:
My app consists of several fragments that are dynammicly added. There is one fragment with a push button and an textedit (called 'fragA').
If I click the push button I want to show an different fragment with some text (called 'fragB'). I do this with the following code (in fragA class):
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment howFragment = new HowFragment();
FragmentTransaction transaction = null;
transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flQuestion, howFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
Now the problem is this:
When i push the button on fragA the fragment (fragB) is created and show on the screen but when I push on the location where the pushbutton was on fragA it makes an new fragment (fragB). Also if i push where the textedit on fragA was located it opens an keyboard on fragB..
It looks like FragB is just overlaying fragA without replacing it.
I also want to achieve that when i swype to the next fragment that fragB is removed and fragA is just showed normally (state when not pressed button)
Update #
When trying to add and remove this is the following logcat output:
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f090015 (com.example.eindwerkappv1:id/flQuestion) for fragment HowFragment{419c93c8 #3 id=0x7f090015}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
I would guess your fragA fragment is defined in your XML layout?
According to the documentation, this is what happens when you use the <fragment> tag:
the system inserts the View returned by the fragment directly in place
of the element.
This is why you cannot remove the previous fragment, as it does not exist.
If you want to changes fragment from code, you have to add the first fragment from code too.
You need to use a container like a FrameLayout, and add the first fragment to this container in the onCreate() of your activity, using FragmentTransaction.add().
Then FragmentTransaction.replace() method should work.
So why don't you just call the .remove() on the EditText Fragment and .add() the new TextView Framgent instead on osing the .replace()
do some thing like this:
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(addCommentFragment)
.add(R.id.containerForFragments, commentFragment, "comment"+
String.valueOf(numOfComments)).commit();
this worked for me.
I found the solution now.
I still use the replace method but I had to catch the ontouch event in the new fragment:
Fragment over another fragment issue
See previous link for the answer

Android Development: java NullPointerException When Trying To getStringExtra()

In my main activity, I have the following code that calls my FileBrowser activity:
Intent newFileIntent = new Intent(getBaseContext(), FileBrowser.class);
newFileIntent.putExtra("action", "browseDirectories");
startActivityForResult(newFileIntent, 2);
But when this code executes, my app force closes.
I ran the app again this time with DDMS open to look for the error, and here's what it is:
11-06 22:01:04.892: ERROR/AndroidRuntime(28287): Caused by: java.lang.NullPointerException
11-06 22:01:04.892: ERROR/AndroidRuntime(28287): at com.alexprice.devpad.FileBrowser.<init>(FileBrowser.java:17)
Here is line 17 (located outside of onCreate):
private String action = getIntent().getStringExtra("action");
What's wrong? Can I not use putExtra with startActivityForResult? Can putExtra only be used with startActivity?
Try moving the declaration inside the onCreate(), or any method, this will ensure you have acess to the intent data. Declaring the variable before onCreate() and thus any other method, you won't have acess to the intent extras.
Leave line 17 as private String action;
And inside onCreate()
action = getIntent().getStringExtra("action");

Categories