我正在開發一個應用程序,只是爲了好玩,而且我對android世界還很陌生。但我想爲我的應用添加一個功能,將背景顏色更改爲黑色或白色,並將文本顏色更改爲黑色或白色。但問題是當我打開應用程序時,它立即崩潰。這是背景變化的MainActivity.java代碼:應用程序的背景更改導致我的應用程序崩潰
RelativeLayout MainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button theme_White = (Button) findViewById(R.id.WhiteTheme);
Button theme_Black = (Button) findViewById(R.id.Blacktheme);
final RelativeLayout MainActivity = (RelativeLayout)findViewById(R.id.MainActivity);
theme_White.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.setBackgroundColor(getResources().getColor(R.color.white));
}
});
theme_Black.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.setBackgroundColor(getResources().getColor(R.color.black));
}
});
setContentView(R.layout.content_main);
theme_White.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView random = (TextView)findViewById(R.id.random);
random.setTextColor(getResources().getColor(R.color.black));
}
});
theme_Black.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView random = (TextView)findViewById(R.id.random);
random.setTextColor(getResources().getColor(R.color.white));
}
});
WhiteTheme是主題更改爲白色背景和黑色文字的按鈕,Blacktheme是主題更改爲黑色背景,白色文本按鈕。 MainActivity是我的activity_main.xml文件的ID。
誰能告訴我如何解決這個崩潰?
謝謝。
[編輯]
Android Studio現在說,這些方法可能會產生'java.lang.NullPointerException'我該如何解決這個問題?
你甚至不會告訴我們錯誤是什麼?至少你應該發佈你的logcat輸出。 – csmckelvey
我不使用模擬器,因爲我的電腦太糟糕了。我直接下載到我的手機並在那裏啓動它 – jepey
因此,您有0調試功能?根本不是一個好的途徑。 – csmckelvey