【发布时间】:2015-03-29 10:49:43
【问题描述】:
我在尝试设置布局的背景颜色时遇到错误,我得到一个空指针异常,我不知道为什么。我查看了各种帖子(例如Change background color on button click?),我的代码似乎正确,但似乎仍然没有调用布局。 这是我的活动代码
public class Color extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color);
findViewById(R.id.blue).setOnClickListener(this);
findViewById(R.id.red).setOnClickListener(this);
findViewById(R.id.pink).setOnClickListener(this);
findViewById(R.id.green).setOnClickListener(this);
findViewById(R.id.yellow).setOnClickListener(this);
findViewById(R.id.light_blue).setOnClickListener(this);
findViewById(R.id.button_blue).setOnClickListener(this);
findViewById(R.id.button_red).setOnClickListener(this);
findViewById(R.id.button_pink).setOnClickListener(this);
findViewById(R.id.button_greend).setOnClickListener(this);
findViewById(R.id.button_yellow).setOnClickListener(this);
findViewById(R.id.button_light_blue).setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.blue:
LinearLayout l1 = (LinearLayout) findViewById(R.id.layout_call);
l1.setBackgroundResource(R.color.blue);
这是 logcat 错误
01-30 01:25:06.828: E/AndroidRuntime(4837): FATAL EXCEPTION: main
01-30 01:25:06.828: E/AndroidRuntime(4837): Process: com.example.primeirocasopratico, PID: 4837
01-30 01:25:06.828: E/AndroidRuntime(4837): java.lang.NullPointerException
01-30 01:25:06.828: E/AndroidRuntime(4837): at com.example.primeirocasopratico.Color.onClick(Color.java:42)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.view.View.performClick(View.java:4463)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.view.View$PerformClick.run(View.java:18770)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.os.Handler.handleCallback(Handler.java:808)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.os.Handler.dispatchMessage(Handler.java:103)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.os.Looper.loop(Looper.java:193)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.app.ActivityThread.main(ActivityThread.java:5327)
01-30 01:25:06.828: E/AndroidRuntime(4837): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 01:25:06.828: E/AndroidRuntime(4837): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 01:25:06.828: E/AndroidRuntime(4837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
01-30 01:25:06.828: E/AndroidRuntime(4837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
01-30 01:25:06.828: E/AndroidRuntime(4837): at dalvik.system.NativeStart.main(Native Method)
我也试过这样设置背景:
findViewById(R.layout.city).setBackgroundColor( android.graphics.Color.parseColor("#0000FF"));
还有其他一些变化,但似乎没有任何效果......
(EDIT 2) 我似乎找到了问题所在。这似乎与我试图编辑与其他活动相关的布局有关。任何想法如何解决这个问题?
【问题讨论】:
-
第 42 行是哪一行?
-
@AlexK
l1.setBackgroundResource(R.color.blue); -
是的。那就看下面我的回答,没错。
-
你的 layout_call LinearLayout 在哪里?
-
@Dhina 在另一个名为
call.java的活动中
标签: android android-linearlayout background-color