首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pi4j无法使用示例代码初始化

pi4j无法使用示例代码初始化
EN

Stack Overflow用户
提问于 2015-01-02 19:03:15
回答 1查看 1.7K关注 0票数 2

我使用的示例代码是直接的从网页上,我仍然得到以下错误:

代码语言:javascript
复制
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

下面是我使用的页面的代码,我更改的只是要测试的类名。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-03 09:58:14

看来您还没有安装本机库(WiringPi)。可以肯定的是,你有没有跟踪这里的一切:http://pi4j.com/install.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27747195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档