繁体   English   中英

Android-findViewById返回null

[英]Android - findViewById returns null

我正在看一本书中的示例,但我不明白为什么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();
    }

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接受三个args。 否则,只需使用super(context, attrs) 哦,从ats重命名arg名称,即使名称无关紧要。

在日食中:

  • 项目->清洁。
  • 刷新您的应用程序。
  • 跑。

这将清除生成的旧R类。

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

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

添加import it.mt.compass.R; 然后尝试其他视图,例如Image或TextView

暂无
暂无

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

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