2015-01-02 44 views
2

我使用的示例代碼直from the webpage初始化,我仍然得到這個錯誤:pi4j無法使用示例代碼

Exception in thread "main" java.lang.ExceptionInInitializerError 
    at com.pi4j.io.gpio.RaspiGpioProvider.<init>(RaspiGpioProvider.java:47) 
    at com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:102) 
    at com.pi4j.io.gpio.impl.GpioControllerImpl.<init>(GpioControllerImpl.java:67) 
    at com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:85) 
    at test.main(test.java:21) 
Caused by: java.lang.NullPointerException 
    at com.pi4j.util.NativeLibraryLoader.load(NativeLibraryLoader.java:218) 
    at com.pi4j.wiringpi.Gpio.<clinit>(Gpio.java:138) 
    ... 5 more 

下面是我使用的頁面的代碼,都讓我改變的是類名來測試。

import com.pi4j.io.gpio.GpioController; 
import com.pi4j.io.gpio.GpioFactory; 
import com.pi4j.io.gpio.GpioPinDigitalInput; 
import com.pi4j.io.gpio.PinPullResistance; 
import com.pi4j.io.gpio.RaspiPin; 
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; 
import com.pi4j.io.gpio.event.GpioPinListenerDigital; 

/** 
* This example code demonstrates how to setup a listener 
* for GPIO pin state changes on the Raspberry Pi. 
* 
* @author Robert Savage 
*/ 
public class test { 

    public static void main(String args[]) throws InterruptedException { 
     System.out.println("<--Pi4J--> GPIO Listen Example ... started."); 

     // create gpio controller 
     final GpioController gpio = GpioFactory.getInstance(); 

     // provision gpio pin #02 as an input pin with its internal pull down resistor enabled 
     final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN); 

     // create and register gpio pin listener 
     myButton.addListener(new GpioPinListenerDigital() { 
      @Override 
      public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { 
       // display pin state on console 
       System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState()); 
      } 

     }); 

     System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console."); 

     // keep program running until user aborts (CTRL-C) 
     for (;;) { 
      Thread.sleep(500); 
     } 

     // stop all GPIO activity/threads by shutting down the GPIO controller 
     // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks) 
     // gpio.shutdown(); <--- implement this method call if you wish to terminate the Pi4J GPIO controller   
    } 
} 

我正在raspbian

+0

在安裝wiringpi之後,Java代碼是否按預期工作?我嘗試了相同的例子(http://pi4j.com/example/listener.html),我的問題是,只要我打開聯繫人,我總是會收到LOW/HIGH事件。 – yglodt

回答