0

我一直得到一个Unable to instantiate activity ComponentInfo... Caused by a NullPointerException at findViewById,我找不到它为什么会抛出它。我有我的元素ID设置正确在我的main.xml和我已经尝试评论每一行周围扔它看哪条线路有故障,但让它运行W/O任何力量关闭我必须注释掉所有的功能该程序。这里是我的活动:找不到这个NullPointerException

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> 

如果有人可以帮助我窄这下来会很好。

+0

你可以把logcat的消息? – Sandy 2011-05-26 14:48:29

回答

9

你只需要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(); 
     } 
    }); 
} 
... 
+0

这就是它非常感谢你 – Joe 2011-05-26 15:01:51

1

将您的通话findViewById到onCreate方法。

1

此实例

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的方法。

2

在调用setContentView(R.layout.main)之后,您需要在onCreate()方法中调用findViewById,因为在将XML扩展到各自的Java对象后,UI对象将不会存在。

0

首先要做的是设置活动的内容,然后,你可以得到所有的这些内容的意见,所以这种替换代码:

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