繁体   English   中英

为什么我的变量是 null?

[英]Why is my variable null?

我有以下代码:

public class Level extends Activity{

    public static String TAG="Level";

    Level_Score_bar score_bar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level_layout);

        score_bar=(Level_Score_bar) findViewById(R.id.Score_Bar);
        Log.d(TAG,"score_bar="+score_bar);
    }
}

XML 代码如下所示(R.layout.level_layout)(删除了不重要的内容)

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
    <pearsonartphoto.AJEG.Level_Score_bar android:id="@+id/Score_Bar" android:layout_height="wrap_content" android:layout_width="fill_parent">
    </pearsonartphoto.AJEG.Level_Score_bar>
</LinearLayout>

日志语句读取“score_bar=null”。可以看到,有一个名为 R.id.Score_Bar 的资源。 我错过了什么吗? 我需要将 function 用于其他一些东西。 谢谢!

有很多事情会导致这种情况。 最常见的是,由于某种原因, R.id没有正确生成。 通常做一个简单的“清理”会解决这个问题。 如果这不起作用,另一件要检查的事情是自定义视图的构造函数。 您可以从这个问题中找到这个和其他建议。

暂无
暂无

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

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