Chrono in der Actionbar bedienen aus Tab

  • Antworten:2
  • Bentwortet
dreadkopp
  • Forum-Beiträge: 23

16.09.2014, 12:13:06 via Website

Aloha Community.

Folgender Aufbau:

Ich habe eine MainActivity, welche mithilfe eines Tabhost Activities in sich darstellt als Tabs.

In der ActionBar der MainActivity habe ich einen Chronometer untergebracht.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="schemas.android_dot_com/apk/res/android" >
<item
android:id="@+id/action_addTab"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/ic_action_addtab"
android:title="addTab"/>
<item
android:id="@+id/action_rmTab"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/ic_action_rmtab"
android:title="rmTab"/>
<item
android:id="@+id/action_back"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/ic_action_name"
android:title="backTab"/>
<item
android:id="@+id/action_setIp"
android:title="Ip ändern">
</item>
<item
android:title=""
android:id="@+id/chronometer"
android:actionLayout="@layout/patient_header"
android:showAsAction="always" android:orderInCategory="100">

</item>
</menu>

wobei das ActionLayout so ausschaut:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android_dot_com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Chronometer
    android:id="@+id/clock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:textColor="#AAAAAA"
    android:format="%s"
    android:textSize="75dp" />

<Spinner
    android:id="@+id/header_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/clock"
    android:layout_centerHorizontal="true"
    android:spinnerMode="dialog" />

</RelativeLayout>

In der MainActivity (über einen testbutton in der Actionbar) kann ich problemlos

public void startTimer(){
Chronometer chrono = (Chronometer) findViewById(R.id.clock);
chrono.start();
}

aufrufen.

Nur möchte ich diese Methode an gewisse Aktionen in anderen Activities binden.

rufe ich aber nun in einem Tab diese Methode auf, bekomme ich eine NullPointerException.

Warum? Der Chrono ist doch fest und über die Id auffindbar?

Wie kann ich mein Problem lösen? Da findViewById nicht statisch verwendet werden kann, kann ich die Methode nicht in der Main lassen und von den anderen Activites aufrufen.

Ich hoffe, ihr versteht mein Problem und könnt mir helfen!

Grüße!

Antworten
Steffen S.
  • Forum-Beiträge: 63

16.09.2014, 12:38:15 via App

probiere es mal mit getParent() vor findViewById

getParent().findViewById( <ID> )

weil das findViewById innerhalb vom Tab bezieht sich nur auf das Layout innerhalb vom Tab

— geändert am 16.09.2014, 12:41:37

Antworten
dreadkopp
  • Forum-Beiträge: 23

16.09.2014, 12:39:54 via Website

HA! du Held! Dankeschön!!!!

Antworten