android - Fragment with Both Activity and FragmentActivity doesn't work -
i want show bar graph in activity in fragment showing inflating issue. so, changed activity fragmentactivity worked application running fragment part output not displaying.
here code unable find out mistake have done:
mainactivity:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <fragment android:id="@+id/fghis" android:layout_width="match_parent" android:layout_height="100dp" class="com.anu.actualdemobargraph.historyfrag" /> </linearlayout>
historyfrag:
import java.util.arraylist; import android.graphics.color; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import com.echo.holographlibrary.bar; import com.echo.holographlibrary.bargraph; import com.echo.holographlibrary.bargraph.onbarclickedlistener; public class historyfrag extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.historyfrag, container,false); bargraph g = (bargraph)view.findviewbyid(r.id.bargraph); arraylist<bar> points = new arraylist<bar>(); bar d = new bar(); d.setcolor(color.parsecolor("#c6c6c6")); d.setname("test1"); d.setvalue(10); bar d2 = new bar(); d2.setcolor(color.parsecolor("#c6c6c6")); d2.setname("test2"); d2.setvalue(20); bar d4 = new bar(); d4.setcolor(color.parsecolor("#c6c6c6")); d4.setname("test2"); d4.setvalue(20); bar d5 = new bar(); d5.setcolor(color.parsecolor("#ffbb33")); d5.setname("test2"); d5.setvalue(20); bar d6 = new bar(); d6.setcolor(color.parsecolor("#c6c6c6")); d6.setname("test2"); d6.setvalue(20); bar d7 = new bar(); d7.setcolor(color.parsecolor("#c6c6c6")); d7.setname("test2"); d7.setvalue(20); bar d8 = new bar(); d8.setcolor(color.parsecolor("#c6c6c6")); d8.setname("test2"); d8.setvalue(20); bar d3 = new bar(); d3.setcolor(color.parsecolor("#c6c6c6")); d3.setname("test3"); d3.setvalue(50); // d3.setstackedbar(true); // d3.addstackvalue(new barstacksegment(2, color.parsecolor("#ffbb33"))); // d3.addstackvalue(new barstacksegment(4, color.red)); points.add(d); points.add(d2); points.add(d3); points.add(d4); points.add(d5); points.add(d6); points.add(d7); points.add(d8); g.setunit("meters"); g.appendunit(true); g.setbars(points); g.setonbarclickedlistener(new onbarclickedlistener(){ @override public void onclick(int index) { } }); return view; } public historyfrag() { // todo auto-generated constructor stub } }
historyfrag.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.echo.holographlibrary.bargraph android:id="@+id/bargraph" android:layout_width="match_parent" android:layout_height="200dp" /> </linearlayout>
in historyfrag.xml, bargraph taken library there no problem in library.
thanks in advance.
you need attach fragment activity using fragmentmanager. depending on whether want use framework fragment or support fragment process differs slightly.
framework fragment
this fragment class built android framework , available in api 11 , above.
to use fragment class part of android framework need change import statement in historyfrag class from:
import android.support.v4.app.fragment;
to
import android.app.fragment;
then in activity oncreate
method need add:
getfragmentmanager() .begintransaction() .replace(r.id.fghis, new historyfrag()) .commit();
support library fragment
if want support api levels <11 need use fragment class support library. backports fragment functionality api 4.
if wanted use fragment class support library need make sure extending fragmentactivity
, leave import statement in historyfrag class android.support.v4.app.fragment
.
then in activity oncreate()
method add like:
getsupportfragmentmanager() .begintransaction() .replace(r.id.fghis, new historyfrag()) .commit();
that attach fragment activity.
Comments
Post a Comment