2012-05-16 68 views
0

我的Android應用程序中有一個非常引人注目的NullPointerException。它是一段代碼,其導致的問題:Extrange 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; 
    } 

} 

感謝您的時間:)

回答

1

這個例外只是意味着它不能啓動活動,因爲它在創建期間引發了異常。實際的問題是在LogicAnalizerView.java行#361上發生的NullPointerException

我猜這是channelProperties[]數組未正確初始化,因爲它看起來像你正在創建數組,但沒有填充任何東西。

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

祝你好運!

+0

謝謝:d你可以看到我在Java的非常新:) – Andres

1

您的行「static LogicChannelProperties [] channelProperties = new LogicChannelProperties [4]」分配4個對象的數組,但它不會自己創建對象。請注意,該語句中沒有構造函數調用 - 只是一個數組分配。

你必須通過數組來創建單個對象實例,例如迭代,

channelProperties[0] = new LogicChannelProperties();