F
fr3sh_pr1nce
Neues Mitglied
- 0
Hallo,
ich nutze den ActionbarSherlock um Fragments in meiner Android App zu verwenden. Nun habe ich das große Problem dass ich nicht in der Lage bin aus der MainActivity aus eine Komponente in einem Fragment zu aktualisieren.
Hier der Status Quo:
MainActivity.java:
Activity_main.xml:
Fragment1.java:
Fragment1.xml:
Ich verstehe nicht, warum ich keine Referenz auf Fragment1 erhalte.
Hätte vielleicht jemand einen Tipp für mich?
Vielen vielen Dank!
ich nutze den ActionbarSherlock um Fragments in meiner Android App zu verwenden. Nun habe ich das große Problem dass ich nicht in der Lage bin aus der MainActivity aus eine Komponente in einem Fragment zu aktualisieren.
Hier der Status Quo:
MainActivity.java:
Code:
public class MainActivity extends SherlockFragmentActivity
{
//Fragments
Fragment1 frag1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
frag1 = new Fragment1();
ft.add(R.id.container_main_top_and_main, frag1, "FNM_TAG");
ft.commit();
TestLoadWebview();
}
private void TestLoadWebview()
{
String HTMLContent = "<html><body>You scored <b>192</b> points.</body></html>";
Fragment1 fnm = (Fragment1)getSupportFragmentManager().findFragmentByTag("FNM_TAG"); //findFragmentByID funktioniert auch nicht...
//FEHLER!! fnm ist immer null
fnm.UpdateWebView(HTMLContent);
}
}
Activity_main.xml:
HTML:
<RelativeLayout 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"
tools:ignore="MergeRootFrame" >
<RelativeLayout
android:id="@+id/container_main_top_and_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/container_main_bottom">
</RelativeLayout>
<RelativeLayout
android:id="@+id/container_main_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
Fragment1.java:
Code:
public class Fragment1 extends SherlockFragment
{
WebView webview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_main_no_mission, container, false);
webview = (WebView)v.findViewById(R.id.my_webview);
return v;
}
public void UpdateWebView(String webviewContent)
{
webview.loadData(webviewContent, "text/html", null);
}
}
Fragment1.xml:
HTML:
<RelativeLayout 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" >
<!-- Top menu -->
<RelativeLayout
android:id="@+id/layout_topmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/clr_main_background"
android:gravity="center|left" >
...
...
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/clr_main_background"
android:gravity="center"
android:layout_below="@+id/layout_topmenu" >
<WebView
android:id="@+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
Ich verstehe nicht, warum ich keine Referenz auf Fragment1 erhalte.
Hätte vielleicht jemand einen Tipp für mich?
Vielen vielen Dank!