user2987274
user2987274

Reputation:

getParent returns wrong ViewGroup

When execute code below I can verify in my LogCat that getParent returns an other ViewGroup then FrameLayout (It returns LinearLayout). Normally I would expect that getParent would return the framelayout...

Does anyone know why this is occuring?

java:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.actionbar_btn);
        if (button.getParent() instanceof FrameLayout) {
            Log.v("TAG", "parent was framelayout");
        } else {
            Log.v("TAG", "parent was no framelayout");
        }

    }

xml:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FF00"
        android:orientation="horizontal" >

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/actionbar_home"
                android:layout_width="33dp"
                android:layout_height="32dp"
                android:background="@drawable/ic_launcher" 
                />


        </FrameLayout>

        .....

    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Views: 637

Answers (2)

Prabhunath Yadav
Prabhunath Yadav

Reputation: 185

I think you are getting wrong id for Button:

 <Button
                    android:id="@+id/actionbar_home"
                    android:layout_width="33dp"
                    android:layout_height="32dp"
                    android:background="@drawable/ic_launcher" 
                    />


final Button button = (Button) findViewById(R.id.actionbar_btn);

"R.id.actionbar_btn" and android:id="@+id/actionbar_home" should be same

Upvotes: 0

Wang
Wang

Reputation: 274

Shouldn't you change your R.id.actionbar_btn to R.id.actionbar_home?

Upvotes: 2

Related Questions