Extrange NullPointerException

时间:2012-05-16 22:35:14

标签: android nullpointerexception

我的Android应用程序上有一个非常冒险的NullPointerException。这是造成问题的代码:

private void setPreferences() {

        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        ReceptionBuffer = new byte[64];     // Buffer de recepcion general de 64 bytes

        // Protocolo para cada canal (I2C, UART, SPI)
        byte[] temp = new byte[4];
        // Canal para el clock de cada canal (en caso de usar protocolos con clock como I2C,SPI)
        byte[] temp2 = new byte[4];

        for(int n=0; n < 4; ++n){
            temp[n] = Byte.decode(getPrefs.getString("protocol" + n, "0"));
            temp2[n] = Byte.decode(getPrefs.getString("channel" + n + "SCL", "0"));
        }
        for(int n=0; n < 4; ++n){
            if(DEBUG) Log.i(TAG, "LogicAnalizerView -> setPreferences() - n: " + n);
            switch(temp[n]){
            case 0:
                channelProperties[n].setProtocol(Protocol.I2C);
                break;
            case 1:
                channelProperties[n].setProtocol(Protocol.UART);
                break;
            }
            channelProperties[n].setClockSource(temp2[n]);
        }

    }

这是LogCat:

05-16 22:28:11.019: I/(965): LogicAnalizerView -> setPreferences() - n: 0
05-16 22:28:11.049: D/AndroidRuntime(965): Shutting down VM
05-16 22:28:11.049: W/dalvikvm(965): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-16 22:28:11.099: I/dalvikvm(965): threadid=3: reacting to signal 3
05-16 22:28:11.332: I/dalvikvm(965): Wrote stack traces to '/data/anr/traces.txt'
05-16 22:28:11.429: E/AndroidRuntime(965): FATAL EXCEPTION: main
05-16 22:28:11.429: E/AndroidRuntime(965): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.multiwork.andres/com.multiwork.andres.LogicAnalizerView}: java.lang.NullPointerException
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.os.Looper.loop(Looper.java:137)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-16 22:28:11.429: E/AndroidRuntime(965):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 22:28:11.429: E/AndroidRuntime(965):  at java.lang.reflect.Method.invoke(Method.java:511)
05-16 22:28:11.429: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-16 22:28:11.429: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-16 22:28:11.429: E/AndroidRuntime(965):  at dalvik.system.NativeStart.main(Native Method)
05-16 22:28:11.429: E/AndroidRuntime(965): Caused by: java.lang.NullPointerException
05-16 22:28:11.429: E/AndroidRuntime(965):  at com.multiwork.andres.LogicAnalizerView.setPreferences(LogicAnalizerView.java:361)
05-16 22:28:11.429: E/AndroidRuntime(965):  at com.multiwork.andres.LogicAnalizerView.onCreate(LogicAnalizerView.java:93)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.Activity.performCreate(Activity.java:4465)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-16 22:28:11.429: E/AndroidRuntime(965):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-16 22:28:11.429: E/AndroidRuntime(965):  ... 11 more
05-16 22:28:11.709: I/dalvikvm(965): threadid=3: reacting to signal 3
05-16 22:28:11.931: I/dalvikvm(965): Wrote stack traces to '/data/anr/traces.txt'
05-16 22:28:12.389: I/dalvikvm(965): threadid=3: reacting to signal 3
05-16 22:28:12.459: I/dalvikvm(965): Wrote stack traces to '/data/anr/traces.txt'
05-16 22:28:13.039: I/dalvikvm(965): threadid=3: reacting to signal 3
05-16 22:28:13.230: I/dalvikvm(965): Wrote stack traces to '/data/anr/traces.txt'
05-16 22:28:17.912: I/Process(965): Sending signal. PID: 965 SIG: 9

根据LogCat的问题在于行channelProperties [n] .setProtocol(Protocol.I2C);. setPreferences()是从onCreate()方法调用的,在此作为全局变量之前我有:

private static LogicChannelProperties[] channelProperties = new LogicChannelProperties[4];

因为我分配了空间,所以很奇怪。为什么我得到这个?这是我的班级:

import com.multiwork.andres.LogicData.Protocol;

public class LogicChannelProperties {

    private Protocol mProtocol;
    private int clockSource;

    /**
     * Setea el protocolo a usar (ej: Protocol.I2C/SPI...)
     * @author Andres Torti
     * @param type es el protocolo a usar (ej: Protocol.I2C/SPI...)
     */
    public void setProtocol (Protocol type){
        mProtocol = type;
    }

    /**
     * Obtiene el protocolo
     * @author Andres Torti
     * @return variable tipo Protocol (ej: Protocol.I2C/SPI...)
     */
    public Protocol getProtocol (){
        return mProtocol;
    }

    /**
     * Setea el canal que actua como fuente de clock
     * @author Andres Torti
     * @param sourceChannel es el numero del canal >0
     */
    public void setClockSource (int sourceChannel){
        clockSource = sourceChannel;
    }

    /**
     * Obtiene el canal que actua como fuente de clock
     * @author Andres Torti
     * @return int, numero del canal
     */
    public int getClockSource (){
        return clockSource;
    }

}

感谢您的时间:)

2 个答案:

答案 0 :(得分:1)

该异常只是意味着它无法启动Activity,因为它在创建期间引发了异常。实际问题是NullPointerException发生在LogicAnalizerView.java的第361行。

我猜测channelProperties[]数组没有正确初始化,因为它看起来像是在创建数组而不是用任何东西填充它。

所以你需要channelProperties[0] = new ChannelProperties(); //等等。

祝你好运!

答案 1 :(得分:1)

你的行“static LogicChannelProperties [] channelProperties = new LogicChannelProperties [4]”分配一个包含4个对象的数组,但它不会自己创建对象。请注意,该语句中没有构造函数调用 - 只是一个数组分配。

您必须遍历数组才能创建单个对象实例,例如

channelProperties[0] = new LogicChannelProperties();