0

我的 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;
    }

}

感谢您的时间 :)

4

2 回答 2

1

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

我猜这channelProperties[]是未正确初始化的数组,因为看起来您正在创建数组但没有用任何东西填充它。

所以你需要像channelProperties[0] = new ChannelProperties();//etc 之类的东西。

祝你好运!

于 2012-05-16T22:43:47.033 回答
1

您的行“static LogicChannelProperties[] channelProperties = new LogicChannelProperties[4]”分配了一个由 4 个对象组成的数组,但它本身并不创建对象。请注意,该语句中没有构造函数调用——只是一个数组分配。

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

channelProperties[0] = new LogicChannelProperties();
于 2012-05-16T22:53:32.960 回答