findViewById()函数返回null

cal*_*e93 -1 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>
Run Code Online (Sandbox Code Playgroud)

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

      }

      });
     */
}
Run Code Online (Sandbox Code Playgroud)

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

Ahm*_*aza 8

改变线条

android:name="@+id/place1"
Run Code Online (Sandbox Code Playgroud)

android:id="@+id/place1"
Run Code Online (Sandbox Code Playgroud)

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