我的 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;
}
}
感谢您的时间 :)