0

I have a view being created based on a boolean option, the view is titled socialoptions which is defined in a separate xml file. The view returns null anytime I am trying to use it through methods such as socialoptions.setClickable(true);.

Java Code:

        if (isUserProfile) {
            Log.d("user","user");
            middlelayout = (LinearLayout) findViewById (R.layout.usermiddlelayout);
        } else {
            Log.d("item","item");
            middlelayout = (LinearLayout)  findViewById (R.layout.itemmiddlelayout);
            socialoptions = (TextView) findViewById (R.id.socialoptions);
            map = (TextView) findViewById (R.id.map);
        }

XML CODE:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemmiddlelayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="25"
    android:background="@android:color/transparent"
    android:clickable="false"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/sociallayout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:background="@android:color/transparent"
        android:paddingBottom="0dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/socialoptions"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:clickable="false" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/maplayout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:paddingBottom="10dp"
        android:paddingTop="0dp" >

        <TextView
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:clickable="false" />
    </RelativeLayout>

</LinearLayout>
Jeremy
  • 3,620
  • 9
  • 43
  • 75
  • Is `middlelayout` also `null`? – Cat Sep 19 '12 at 19:40
  • Yes, middlelayout is also null. – Jeremy Sep 19 '12 at 19:58
  • I suspect, then, that you are not referencing this from the root view, or from the `Activity` in which `setContentView` is called. Try calling `findViewById(android.R.id.content)` and see if that is `null`. – Cat Sep 19 '12 at 19:58
  • The content view is set to a separate xml then the one holding the middlelayout, I was hoping to call it despite not being there. In the same way you can reference drawables across various files. – Jeremy Sep 19 '12 at 20:01

2 Answers2

1

As per comments:

You cannot do what you're trying to do; you cannot reference an XML layout which has not been set up. That is to say, you must use setContentView or inflate such a layout first.

A drawable is stored in the application as a referenceable object; it exists as a "file" within your APK. A layout is just data that means nothing until it is "created".

Once you set the content view or inflate your layout into view, you will not receive null objects anymore when referencing the layout's contents.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
0

If the xml page is saved as usermiddlelayout.xml, you can use R.layout.usermiddlelayout. If the linear layout inside the xml file set it's id to android:id="@+id/itemmiddlelayout" you should use R.id.itemmiddlelayout and not R.layout.itemmiddlelayout.

Here,

middlelayout = (LinearLayout) findViewById (R.id.itemmiddlelayout);

praveenLal
  • 94
  • 2
  • 12
  • This solved a separate problem by not making the middlelayout return null but the socialoptions still does. Thanks for helping. – Jeremy Sep 19 '12 at 20:11