Fragment Transaction not recognizing my fragment as a fragment - java

So i'm implementing a FragmentActivity and am trying to add a fragment, however I'm running into multiple problems. I've done this before, and am actually using the same code as the last project (where it worked), but for some reason it's not working here. Here's my code:
package silversphere.eyeon;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.LinearLayout;
public class AlarmsActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setId(40000);
FragmentManager man = getFragmentManager();
FragmentTransaction trans = man.beginTransaction();
Alarm_Landing_Page_Fragment frag = new Alarm_Landing_Page_Fragment();
trans.add(40000, frag);
}
}
I'm getting an error on the layout.setId() call and also the trans.add(). The layout.setId() is saying "expected resource of type id" and the transaction is saying "cannot resolve method add(int, silversphere.eyeon.alarm_landing_page_fragment)", which to me suggests that for some reason it's not recognizing the Alarm_LandingPage_Fragment as a fragment, but it definitely should be. Here's the declaration for Alarm_Landing_Page_Fragment:
public class Alarm_Landing_Page_Fragment extends Fragment {
Any suggestions are appreciated!

You are mixing Fragment related classes from the support library with the ones shipped with SDK. Use either only support library classes or only SDK's built-in classes when working with Fragments (I mean only classes related to Fragments).
If your class Alarm_Landing_Page_Fragment extends Fragment from the support library then change:
import android.app.FragmentManager;
import android.app.FragmentTransaction;
// ...
FragmentManager man = getFragmentManager();
to:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
// ...
FragmentManager man = getSupportFragmentManager();
Otherwise, do not extend FragmentActivity and use only Fragment related classes from SDK.
Edit:
See also this answer:
Error inflating class fragment (As ListFragment)

The problem is related to the setId. You have to create an <id> resource:
/res/values/ids.xml
<resources>
<item type="id" name="myId" />
</resources>
An then, on your file:
LinearLayout layout = new LinearLayout(this);
layout.setId(R.id.myId);
...
trans.add(R.id.myId, frag);

Related

How to split the screen in more then one part

I am trying to implement an app that plays RTMP url which I have done already using libvlc sdk.
Now I want to split the screen more than one part and want to play rtmp url I am facing problem with spliting the screen any suggestions will be appreciated thanks in advance
To Split Android activity , You should use fragment class which allows you to show multiple task dynamically. You can use fragment class .
Steps :
1. Create Fragment class
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class yourFragmentName extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
Create FrameLayout in your activity where you want to show fragment.
<FrameLayout
android:id="#+id/your_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
In your Main Activity , add this layout to fragment
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder, new FooFragment());
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commit();
For the second fragment also repeat same.
For more explanation refer , https://guides.codepath.com/android/Creating-and-Using-Fragments
With the help of fragments you can slit the screen into multiple part and each one will have its own UI

How to call dialogfragment from another fragment?

I'm really new in android development, hope you guys can help me in my problem. I had already search for any solution but none of it works.
I have 6 fragments for scrollable tabs, then each tabs has ADD TO CART buttons, if I click that button a fragment dialog should appear. In my case, there is this error.
Error:(37, 13) error: no suitable method found for show(android.support.v4.app.FragmentManager,String)
method DialogFragment.show(android.app.FragmentManager,String) is not applicable
(argument mismatch; android.support.v4.app.FragmentManager cannot be converted to android.app.FragmentManager)
method DialogFragment.show(FragmentTransaction,String) is not applicable
(argument mismatch; android.support.v4.app.FragmentManager cannot be converted to FragmentTransaction)
Heres my code by the way for the fragment to call the DialogFragment.
package info.androidhive.materialtabs.fragments;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import info.androidhive.materialtabs.R;
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
public void toDiagCartFragment(View v){
FragmentManager manager = getFragmentManager();
CartFragment cart = new CartFragment();
cart.show(manager, "My Cart");
}
}
This the code for DialogFragment to be called by OneFragment
package info.androidhive.materialtabs.fragments;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import info.androidhive.materialtabs.R;
public class CartFragment extends DialogFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_cart2, null);
}
}
Seems like the error is in the OneFragment.java
Heres my output by the way.
_I really appreciate for any answers, just please be nice to me
.I really don't know how to do it . :(
.Thanks :)
your error says your answer clearly.
type mismatch
your dialogFragment has android.app.FragmentManager and you are calling android.support.v4.app.FragmentManager. you should use getSupportFragmentManger() insted of getFragmentManager();
first of all replace your cartFragment import to this,
import android.support.v4.app.DialogFragment;
and create this method.
public static CartFragment newInstance() {
CartFragment dialog = new CartFragment ();
return dialog;
}
and in your one_fragment use like this.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
CartFragment newFragment = CartFragment .newInstance();
newFragment.show(ft, "My Cart");
your OneFragment belongs to support library fragment while CartFragment doesn't. you need to either modify CartFragment to support library or OneFragment to default Fragment. and modifying CartFragment to support library fragment will be better as support library has more functionality.
There is a version mismatch. Instead of using getFragmentManager(), use getSupportFragmentManager() as you are using the support library version of Fragments.
Replace
FragmentManager manager = getFragmentManager();
with
FragmentManager manager = getSupportFragmentManager();

getSupportFragmentManager() is not working on onCreateView() method of the fragment class.

Goodevening. I have read several questions here in stackoverflow and some tutorial articles and site about fragments. I'm currently passing some data on the first fragment class to the second fragment class. But I've got some error on the part of transaction in the bundle, It says in the error that the method getSupportFragmentManager() method is undefined.
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, cblf).commit();
What is the bestway to fix this problem?
Anyways, here is my whole code,
package com.example.navigationdrawerexample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class CollegeBulletinFragment extends Fragment implements OnClickListener {
Button ccs;
Button coe;
Button coed;
Button con;
Button cba;
Button cas;
Button cihm;
Button gn;
public CollegeBulletinFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collegebulletin, container, false);
gn = (Button) rootView.findViewById(R.id.gn);
ccs = (Button) rootView.findViewById(R.id.ccs);
coe = (Button) rootView.findViewById(R.id.coe);
coed = (Button) rootView.findViewById(R.id.coed);
con = (Button) rootView.findViewById(R.id.con);
cba = (Button) rootView.findViewById(R.id.cba);
cas = (Button) rootView.findViewById(R.id.cas);
cihm = (Button) rootView.findViewById(R.id.cihm);
gn.setOnClickListener(this);
ccs.setOnClickListener(this);
coe.setOnClickListener(this);
coed.setOnClickListener(this);
con.setOnClickListener(this);
cba.setOnClickListener(this);
cas.setOnClickListener(this);
cihm.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
String passingword = "";
switch (arg0.getId()){
case R.id.ccs:
passingword = "CCS";
break;
case R.id.coe:
passingword = "COE";
break;
case R.id.gn:
passingword = "GN";
break;
case R.id.coed:
passingword = "COED";
break;
case R.id.con:
passingword = "CON";
break;
case R.id.cba:
passingword = "CBA";
break;
case R.id.cas:
passingword = "CAS";
break;
case R.id.cihm:
passingword = "CIHM";
break;
}
CollegeBulletinListFragment cblf = new CollegeBulletinListFragment();
Bundle args = new Bundle();
args.putString("passingWord", passingword);
cblf.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, cblf).commit();
}
}
error that the method getSupportFragmentManager() method is undefined
Because getSupportFragmentManager() method is not available in Fragment class it is in FragmentActivity so use getActivity() to access it.like:
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, cblf).commit();
getSupportFragmentManager() is defined on AppCompatActivity to avoid clashing with the native (non-support) getFragmentManager(). However, for the support Fragment class, there's no existing method to clash with, so you should instead use:
getFragmentManager()
to get the hosting Activity's FragmentManager, or alternatively, use:
getChildFragmentManager()
to the the Fragment's own FragmentManager (for nesting Fragments)
You have to use the support library android.support.v4... for using getSupportFragmentManager().
There are two types of Fragment, "regular", for which the import declaration is android.app.Fragment, and "support", which is android.support.v4.app.Fragment. Your Fragment isn't a "support" Fragment, evidenced by the import declaration in your code:
package com.example.navigationdrawerexample;
**import android.app.Fragment;**
import android.os.Bundle;
Second, you access getSupportFragmentManager() from a FragmentActivity (and thus any class that extends FragmentActivity, such as ActionBarActivity and AppCompatActivity)
So, to begin with, you should change your import declaration to android.support.v4.app.Fragment.
Then, you should ensure the Activity that hosts the Fragment is a FragmentActivity (or AppCompatActivity etc.)
Finally, inside your Fragment, you should be able to call getActivity().getSupportFragmentManager().
You can also browse the documentation for Communicating with Other Fragments, as I would personally execute the code in your question from the Activity, not the Fragment itself, via an interface (here's a link to a related question that shows this technique).

Object specify error

I am trying to code an action bar for my android app and i defined my object to set the logo of the action bar to the default andorid icon before i update. When i run just that code, i get an error on the marked line.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class myabDemo extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myab_demo);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher); // ***Error appears here***
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
It's not R.drawable.icon_launcher but R.drawable.ic_launcher (At least i'm pretty sure you followed the tutorial that puts ic_launcher and not icon_launcher)
I guess you received a nullpointer exception, since actionBar is null.
Add the code below:
requestWindowFeature(Window.FEATURE_NO_TITLE);

Different behavior of android.app.Fragment and android.support.v4.app.Fragment while using backstack

I've created Activity and added a fragment to it using FragmentManeger. When i use android.app.Fragment and press the back button my application closes. When i use android.support.v4.app.Fragment and press the back button, the fragment is removed from the activity, but application is still working. I can't really understand why is that happening.
Here is the code i used:
Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.content_fragment, new Fragment1())
.addToBackStack("first")
.commit();
}
}
Fragment:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
When i simply replace import in Activity and Fragment to same classes, but in the support library, the result is different...
EDIT:
I also replaced getFragemetManeger() to getSupportFragmentMeneger() and it still works different
if you're only looking to change the back behavior, you can try overriding
onBackPressed with something like:
if(backstackCount() > 0)
{
super.onBackPressed
}
The issue occured because i used android.support.v7.app.AppCompatActivity and android.app.Fragment. I should've used android.app.Fragment with android.app.Activity or AppCompatActivity with support fragment.

Categories