简体   繁体   中英

Android: View returning null when called?

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>

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM