代码中的按钮图像更改错误

时间:2012-04-19 03:10:20

标签: android

按钮图像更改的Java代码,按钮图像更改的Java代码

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);



        Button btn=(Button)findViewById(R.id.level1);
            btn.setBackgroundColor(Color.BLUE);




        // setContentView(new Gameview(this)`enter code here`);
        setContentView(R.layout.levels);
        addListenerOnButton();


    }

存在按钮布局,按钮布局存在

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/levelbackground"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.10" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/level1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/level1"
                android:height="79dp"
                android:width="80dp" />

            <Button
                android:id="@+id/level2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/levelloked"
                android:height="79dp"
                android:width="80dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/level3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/levelloked"
                android:height="79dp"
                android:width="80dp" />

            <Button
                android:id="@+id/level4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/levelloked"
                android:height="79dp"
                android:width="80dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/level5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/levelloked"
                android:height="79dp"
                android:width="80dp" />

            <Button
                android:id="@+id/level6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/levelloked"
                android:height="79dp"
                android:width="80dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >
        </TableRow>
    </TableLayout>F

</LinearLayout>

请帮助这个不起作用?我想在代码中更改按钮背景图像?我得到零点异常...

错误记录,错误记录,错误记录

04-19 08:28:43.279: I/ApplicationPackageManager(3716): cscCountry is not German : XSG
04-19 08:28:43.279: D/AndroidRuntime(3716): Shutting down VM
04-19 08:28:43.279: W/dalvikvm(3716): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-19 08:28:43.289: E/AndroidRuntime(3716): FATAL EXCEPTION: main
04-19 08:28:43.289: E/AndroidRuntime(3716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hunter99x/com.hunter99x.LevelsScreen}: java.lang.NullPointerException
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.os.Looper.loop(Looper.java:123)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at java.lang.reflect.Method.invoke(Method.java:507)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at dalvik.system.NativeStart.main(Native Method)
04-19 08:28:43.289: E/AndroidRuntime(3716): Caused by: java.lang.NullPointerException
04-19 08:28:43.289: E/AndroidRuntime(3716):     at com.hunter99x.LevelsScreen.onCreate(LevelsScreen.java:21)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 08:28:43.289: E/AndroidRuntime(3716):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-19 08:28:43.289: E/AndroidRuntime(3716):     ... 11 more

3 个答案:

答案 0 :(得分:1)

首先加载xml文件,然后挂载所有其他组件。

setContentView(R.layout.levels);

Button btn=(Button)findViewById(R.id.level1);
btn.setBackgroundColor(Color.BLUE);

答案 1 :(得分:1)

在设置布局之前,不能使用任何布局元素。所以使用下面的代码

setContentView(R.layout.levels);
Button btn=(Button)findViewById(R.id.level1);
btn.setBackgroundColor(Color.BLUE);

答案 2 :(得分:0)

Button btn=(Button)findViewById(R.id.level1);
btn.setBackgroundColor(Color.BLUE);

粘贴以下两行

setContentView(R.layout.levels);

我们需要先设置内容视图然后再执行初始化任务。