简体   繁体   English

findViewById()函数返回null

[英]findViewById() function returns null

I have layout file custom_lock.xml. 我有布局文件custom_lock.xml。 It has 3 child layouts. 它具有3个子布局。 Problem is when I try to access the elements of the layout from MainActivity.java, it returns null. 问题是,当我尝试从MainActivity.java访问布局的元素时,它返回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 以下是我的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. 当我运行该程序时,“ placexml”会打印在控制台上。 I don't know, why this is happening. 我不知道为什么会这样。 place1 is returning null from xml. place1从xml返回null。 Please help. 请帮忙。

Change lines 换线

android:name="@+id/place1"

to

android:id="@+id/place1"

You need to set ID instead of name. 您需要设置ID而不是名称。

setContentView(R.layout.custom_lock);

您的布局名称“ custom_layout”错误吗?

Try changing 尝试改变

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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