微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到此NullPointerException

如何解决找不到此NullPointerException

| 我不断收到
Unable to instantiate activity ComponentInfo... Caused by a NullPointerException at findViewById
,我不知道为什么扔它。我在我的ѭ1correctly中正确设置了我的元素id,并且我尝试注释掉抛出错误的每一行,以查看哪一行是有问题的,但是要使其不带任何强制闭合运行,我必须全部注释掉程序的功能。这是我的活动:
package fsg.dev.test.checkBoxtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedchangelistener;
import android.widget.SeekBar;

public class checkBoxTest extends Activity {
    private CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
    //private SeekBar seekbar = (SeekBar) findViewById(R.id.seekbar);
    private View view = (View) findViewById(R.id.background);

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    checkBox.setonCheckedchangelistener(new OnCheckedchangelistener(){
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
            changeColor();
        }
    });
}

private void changeColor(){
    if(checkBox.isChecked()){
        view.setBackgroundColor(R.color.Blue);
        //view.setAlpha(.25);
    }
    else if(!checkBox.isChecked()){
        view.setBackgroundColor(R.color.White);
    }
}
} 这是我的清单:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
  package=\"fsg.dev.test.checkBoxtest\"
  android:versionCode=\"1\"
  android:versionName=\"1.0\">
<uses-sdk android:minSdkVersion=\"10\" />

<application android:icon=\"@drawable/icon\" android:label=\"@string/app_name\" android:debuggable=\"true\">
    <activity android:name=\".checkBoxTest\"
              android:label=\"@string/app_name\">
        <intent-filter>
            <action android:name=\"android.intent.action.MAIN\" />
            <category android:name=\"android.intent.category.LAUNCHER\" />
        </intent-filter>
    </activity>
</application>
</manifest>
最后是我的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=\"fill_parent\"
android:layout_height=\"fill_parent\">

<CheckBox 
    android:text=\"Background Color\" 
    android:layout_width=\"wrap_content\" 
    android:layout_height=\"wrap_content\" 
    android:checked=\"false\"
    android:id=\"@+id/checkBox\">
</CheckBox>
<SeekBar 
    android:layout_height=\"wrap_content\" 
    android:layout_width=\"fill_parent\"
    android:id=\"@+id/seekbar\" 
    android:layout_margin=\"10dip\" >
</SeekBar>
<View
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\"
    android:background=\"@color/White\"
    android:id=\"@+id/background\">
</View>
</LinearLayout>
如果有人可以帮助我缩小范围,那将是很好的。     

解决方法

您只需在
setContentView
之后使用
findViewById
....
public class checkBoxTest extends Activity {
    private CheckBox checkbox;
    private View view;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    checkbox = (CheckBox) findViewById(R.id.checkbox);
    view = (View) findViewById(R.id.background);
    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
            changeColor();
        }
    });
}
...
    ,调用setContentView(R.layout.main)后,需要在onCreate()方法中调用findViewById,因为在XML扩展到其各自的Java对象之后,UI对象才存在。     ,将对findViewById的调用移到OnCreate方法中。     ,这个实例化
private CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
    //private SeekBar seekbar = (SeekBar) findViewById(R.id.seekbar);
    private View view = (View) findViewById(R.id.background);
应该进来onCreate的方法。     ,首先要做的是设置活动的内容,然后,您可以从该内容中获取所有视图,因此将代码替换为:
public class checkBoxTest extends Activity {
    private CheckBox checkbox ;
    private View view ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
//first,set the content of your activity
        setContentView(R.layout.main);
    //Second,get yours Views 
     checkbox = (CheckBox) findViewById(R.id.checkbox);
     view = (View) findViewById(R.id.background);

        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
                changeColor();
            }
        });
    }
....
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?