在code按钮图像更改错误

在code按钮图像更改错误

本文介绍了在code按钮图像更改错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮图像的

Java的code更改,按钮图像改变的Java code

 公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);        按钮BTN =(按钮)findViewById(R.id.level1);
            btn.setBackgroundColor(Color.BLUE);
        //的setContentView(新Gameview(本)`输入code here`);
        的setContentView(R.layout.levels);
        addListenerOnButton();
    }

按钮的布局存在时,按钮的布局存在

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@绘制/ levelbackground
    机器人:重力=center_vertical
    机器人:方向=横向>    < TableLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1.10>        <的TableRow
            机器人:ID =@ + ID / tableRow1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=CENTER_HORIZONTAL>            <按钮
                机器人:ID =@ + ID / 1级
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginRight =10dp
                机器人:layout_marginTop =10dp
                机器人:背景=@绘制/ 1级
                机器人:身高=79dp
                机器人:宽=80dp/>            <按钮
                机器人:ID =@ + ID / 2级
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginRight =10dp
                机器人:layout_marginTop =10dp
                机器人:背景=@绘制/ levelloked
                机器人:身高=79dp
                机器人:宽=80dp/>
        < /&的TableRow GT;        <的TableRow
            机器人:ID =@ + ID / tableRow2
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=CENTER_HORIZONTAL>            <按钮
                机器人:ID =@ + ID / Level3的
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginRight =10dp
                机器人:layout_marginTop =10dp
                机器人:背景=@绘制/ levelloked
                机器人:身高=79dp
                机器人:宽=80dp/>            <按钮
                机器人:ID =@ + ID /级别4
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginRight =10dp
                机器人:layout_marginTop =10dp
                机器人:背景=@绘制/ levelloked
                机器人:身高=79dp
                机器人:宽=80dp/>
        < /&的TableRow GT;        <的TableRow
            机器人:ID =@ + ID / tableRow3
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=CENTER_HORIZONTAL>            <按钮
                机器人:ID =@ + ID / LEVEL5
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginRight =10dp
                机器人:layout_marginTop =10dp
                机器人:背景=@绘制/ levelloked
                机器人:身高=79dp
                机器人:宽=80dp/>            <按钮
                机器人:ID =@ + ID / 6级
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginRight =10dp
                机器人:layout_marginTop =10dp
                机器人:背景=@绘制/ levelloked
                机器人:身高=79dp
                机器人:宽=80dp/>
        < /&的TableRow GT;        <的TableRow
            机器人:ID =@ + ID / tableRow4
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=CENTER_HORIZONTAL>
        < /&的TableRow GT;
    < / TableLayout>˚F< / LinearLayout中>

请帮助这个不工作?我想改变code按钮背景图片??我拿到零点例外...

的错误的日志,错误的日志,错误日志

  8月4日至19日:28:43.279:I / ApplicationPackageManager(3716):cscCountry是不是德国:XSG
八月四日至19日:28:43.279:D / AndroidRuntime(3716):关闭VM
八月四日至19日:28:43.279:W / dalvikvm(3716):主题ID = 1:螺纹未捕获的异常(组= 0x40018578)退出
八月四日至19日:28:43.289:E / AndroidRuntime(3716):致命异常:主要
八月四日至19日:28:43.289:E / AndroidRuntime(3716):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.hunter99x / com.hunter99x.LevelsScreen}:显示java.lang.NullPointerException
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.os.Handler.dispatchMessage(Handler.java:99)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.os.Looper.loop(Looper.java:123)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.ActivityThread.main(ActivityThread.java:3687)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在java.lang.reflect.Method.invokeNative(本机方法)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在java.lang.reflect.Method.invoke(Method.java:507)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在dalvik.system.NativeStart.main(本机方法)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):因:显示java.lang.NullPointerException
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在com.hunter99x.LevelsScreen.onCreate(LevelsScreen.java:21)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
八月四日至19日:28:43.289:E / AndroidRuntime(3716):11 ...更多


解决方案

 按钮BTN =(按钮)findViewById(R.id.level1);
btn.setBackgroundColor(Color.BLUE);

粘贴下面

上面两行

 的setContentView(R.layout.levels);

我们需要首先设置内容视图然后做初始化任务。

Java code of the button image changing,Java code of the button image changing

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();


    }

Layout of the button exist,Layout of the button exist

<?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>

Please help this not working? I want to change the button background image in code??I get null point exception...

Log of the error,Log of the error,Log of the error

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
解决方案
Button btn=(Button)findViewById(R.id.level1);
btn.setBackgroundColor(Color.BLUE);

paste the above two lines below

setContentView(R.layout.levels);

we need to first set content view then do the initialization task.

这篇关于在code按钮图像更改错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:50