【问题标题】:Android - findViewById returns nullAndroid - findViewById 返回 null
【发布时间】:2014-01-09 23:11:53
【问题描述】:

我正在按照书中的示例进行操作,但我不明白为什么 findViewById 返回 null。

这是我的活动:

package it.mt.compass;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class CompassActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        CompassView cv = (CompassView)this.findViewById(R.id.compassView1);

        // this crashes the application
        //cv.setBearing(45);

        // some debug code
        Toast test_result;

        if(cv == null) {
            test_result = Toast.makeText(this, "1", Toast.LENGTH_SHORT);
            test_result.show();
        }
        else {
            test_result = Toast.makeText(this, "0", Toast.LENGTH_SHORT);
            test_result.show();
        }

        // it shows 1
    }
}

这是 res/layout/main.xml

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

    <it.mt.compass.CompassView 
        android:id="@+id/compassView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
</LinearLayout>

已经清理(如其他类似主题中所建议的那样;“清理”有什么作用?)项目没有运气。

提前非常感谢。 米尔科

根据要求,构造函数的代码:

// Constructors


public CompassView(Context context) {
        super(context);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet attrs) {
        super(context);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet ats, int defaultStyle) {
        super(context);
        initCompassView();
    }

这是正确的版本(问题是我没有将参数正确传递给超类构造函数):

// Constructors


public CompassView(Context context) {
        super(context);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet ats, int defaultStyle) {
        super(context, ats, defaultStyle);
        initCompassView();
    }

【问题讨论】:

  • 错误说明了什么?
  • 没有罗盘视图的res/layout-{modifier}/main.xml 的任何资源变体?
  • 尝试不使用“this”。只有 CompassView cv = (CompassView) findViewById(R.id.compassView1);
  • @El_Mochiq 这段代码不会出错,但如果我尝试使用 cv 我会得到一个空指针异常
  • 您能否发布您的 CompassView 构造函数代码 - 可能属性未正确传递给超类 ctor。

标签: android view findviewbyid


【解决方案1】:

CompassView 构造函数实现不正确。您没有将属性传递给超类,因此 id 丢失了。

在此处更改超类构造函数调用

public CompassView(Context context, AttributeSet attrs) {
    super(context);

super(context, attrs);

public CompassView(Context context, AttributeSet ats, int defaultStyle) {
    super(context);

super(context, attrs, defaultStyle); 如果超类有一个接受三个参数的ctor。否则只需使用super(context, attrs)。哦,将 arg 名称从 ats 重命名,即使名称无关紧要。

【讨论】:

  • 再次感谢您,这成功了。
【解决方案2】:

在eclipse中做:

  • 项目 -> 清理。
  • 让您的应用焕然一新。
  • 运行。

这将清除生成的旧 R 类。

【讨论】:

  • 感谢您的回复。我尝试使用 Clean,但它仍然返回 null。
【解决方案3】:

我会尝试完全关闭 Eclipse,然后再次打开它。我已经看到一些非常奇怪的事情发生了。您可以尝试的另一种方法是添加一个“textView”并尝试对其执行 findById 并查看它是否返回 null。您可能正在加载错误的 xml 视图..

即:您正在从一个目录加载布局,但它实际上是在具有相同名称的不同目录中加载不同的视图...

【讨论】:

  • 我重新启动了机器。它没有用。我添加了一个 TextView 并且 findViewById 可以正常使用它。
【解决方案4】:

添加import it.mt.compass.R; 并尝试另一个视图,例如 Image 或 TextView

【讨论】:

  • 我添加了一个 TextView 并且 findViewById 可以正常使用它,但我的 CompassView 仍然没有运气
  • 其实是构造函数出错,我没有把所有的参数都传给超类的构造函数。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
相关资源
最近更新 更多