Search code examples
androidfindviewbyid

findViewById() function returns null


I have layout file custom_lock.xml. It has 3 child layouts. Problem is when I try to access the elements of the layout from MainActivity.java, it returns null. Its driving me nuts. Please help me. Thanks.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp" >

        <TextView
            android:name="@+id/place1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:gravity="center"
            android:text="Place"
            android:textColor="#000000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:name="@+id/listofapps"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_light"
            android:text="List of Apps" />

        <ListView
            android:name="@+id/sites"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_green_light" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:name="@+id/calories"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_purple"
            android:text="Health Data" />

        <ListView
            android:name="@+id/dialer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray" />
    </LinearLayout>

</LinearLayout>

Following is my MainActivity.java

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.custom_lock);

    startService(new Intent(this, MyService.class));

    TextView placexml = (TextView) findViewById(R.id.place1);

    if (placexml == null)
        System.out.println("placexml");

    String place = "work";
    //System.out.println("place: "+place);
    placexml.setText(place);

    boolean isApiRunning = ContextSdk
            .isSemusiSensing(getApplicationContext());
    if (isApiRunning)
        Api.stopContext();
    else {
        SdkConfig config = new SdkConfig();
        config.setPlacesAccuracyLevel(PlacesAccuracyLevel.EAccuracyHigh);
        config.setActivityAccuracyLevel(ActivityAccuracyLevel.EAccuracyHigh);
        config.setActivityTrackingAllowedState(true);
        config.setAnalyticsTrackingAllowedState(true);
        config.setDemographicsTrackingAllowedState(true);
        config.setPedometerTrackingStateAllowed(true);
        config.setPlacesTrackingAllowedState(true);
        config.setRuleEngineEventStateAllowed(true);

        Api.startContext(getApplicationContext(), config);
    }

    // reloadRssHandler.postDelayed(relaodRssRunnable, time);

    //updateUILayer();

    /*
      txt1.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View arg0) { // TODO Auto-generated
      method stub System.out.println("finishing......."); finish();

      }

      });
     */
}

When I run this program, "placexml" gets printed on console. I don't know, why this is happening. place1 is returning null from xml. Please help.


Solution

  • Change lines

    android:name="@+id/place1"
    

    to

    android:id="@+id/place1"
    

    You need to set ID instead of name.