简体   繁体   English

为什么我的变量是 null?

[英]Why is my variable null?

I have the following code:我有以下代码:

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);
    }
}

The XML code looks like this (R.layout.level_layout) (Non-important stuff removed) 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>

The log statement reads "score_bar=null".As you can see, there is a resource named R.id.Score_Bar.日志语句读取“score_bar=null”。可以看到,有一个名为 R.id.Score_Bar 的资源。 Am I missing something?我错过了什么吗? I need to use the function for some other stuff.我需要将 function 用于其他一些东西。 Thanks!谢谢!

There are a number of things that can cause this.有很多事情会导致这种情况。 The most common being that for one reason or another R.id was not generated correctly.最常见的是,由于某种原因, R.id没有正确生成。 Usually doing a simple 'clean' will fix this.通常做一个简单的“清理”会解决这个问题。 If that doesn't work another thing to check would be the constructor for the custom view.如果这不起作用,另一件要检查的事情是自定义视图的构造函数。 You can find this and other suggestions from this question .您可以从这个问题中找到这个和其他建议。

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

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