【发布时间】:2016-04-22 14:33:39
【问题描述】:
我正在开发一个应用程序只是为了好玩,而且我对 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 输出。
-
我不使用模拟器,因为我的电脑太糟糕了。我直接把它下载到我的手机上并在那里启动它
-
那么你的调试能力是0?根本不是一条好路。
-
是的,我想是的,但我真的不知道除了手机还能用什么
标签: android onclick crash onclicklistener