findViewById()函数返回null

时间:2014-06-25 12:26:39

标签: android findviewbyid

我有布局文件custom_lock.xml。它有3个子布局。问题是当我尝试从MainActivity.java访问布局的元素时,它返回null。它让我疯狂。请帮我。感谢。

<?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>

以下是我的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();

      }

      });
     */
}

当我运行此程序时,“placexml”会在控制台上打印出来。我不知道,为什么会这样。 place1从xml返回null。请帮忙。

3 个答案:

答案 0 :(得分:8)

更改行

android:name="@+id/place1"

android:id="@+id/place1"

您需要设置ID而不是名称。

答案 1 :(得分:0)

setContentView(R.layout.custom_lock);

布局名称“custom_layout”错误?

答案 2 :(得分:0)

尝试更改

<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" />

<TextView
        android:id="@+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" />