我一直得到一個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>
如果有人可以幫助我窄這下來會很好。
你可以把logcat的消息? – Sandy 2011-05-26 14:48:29