android - 从 URL 读取 JSON 到 Android

标签 android json url

我现在开始使用 Java/Android,但出现此错误...

正在关注 this教程(根据我的需要修改),我想连接到 rumonet.pt/feeder/index_android.php 输出一个 json 数组。

但该代码在

中给了我一个错误
  button = (Button) findViewById(R.id.btnFetch);
        buttonClear = (Button) findViewById(R.id.btnClear);
        textView = (TextView) findViewById(R.id.txtView);
        button.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
               // examineJSONFile();
            }
        });
        buttonClear.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                //textView.setText("");
            }
        });

更具体地说,在按钮和按钮清除 SetOnClickListener 中,因为如果我评论那些行和空白,它会起作用...只是什么都不做...

任何人都知道为什么,或者有更好的教程? =) 谢谢 =)

编辑

11-09 14:16:31.836: D/AndroidRuntime(9939): Shutting down VM
11-09 14:16:31.836: W/dalvikvm(9939): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
11-09 14:16:31.836: E/AndroidRuntime(9939): FATAL EXCEPTION: main
11-09 14:16:31.836: E/AndroidRuntime(9939): java.lang.RuntimeException: Unable to start activity ComponentInfo{noticias.rumonet.pt/noticias.rumonet.pt.MainActivity}: java.lang.NullPointerException
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.os.Looper.loop(Looper.java:137)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.main(ActivityThread.java:4441)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at dalvik.system.NativeStart.main(Native Method)
11-09 14:16:31.836: E/AndroidRuntime(9939): Caused by: java.lang.NullPointerException
11-09 14:16:31.836: E/AndroidRuntime(9939):     at noticias.rumonet.pt.MainActivity.onCreate(MainActivity.java:85)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.Activity.performCreate(Activity.java:4465)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
11-09 14:16:31.836: E/AndroidRuntime(9939):     ... 11 more







   button.setOnClickListener(new Button.OnClickListener()
                {
                    public void onClick(View v)
                    {
                       // examineJSONFile();
                    }
                });

最佳答案

检查 XML 布局。它真的叫 btnClear 吗??

在我看来,一旦您尝试访问 onClickListener,您就会得到一个空指针,因为它在布局上找不到 View 。

关于android - 从 URL 读取 JSON 到 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13309619/

相关文章:

ios - 如何使用flutter,wordpress在我的应用程序中添加拉动刷新?

ios - 将 NSString(文件路径)包装在 NSURL 中会破坏 Unicode 吗?

android - 搜索小部件中的自定义搜索建议

java - 错误 : the constructor intent is undefined

javascript - Json stringify范围错误

c# - 如何去除json字符串中的空值

r - 如何确定 url 对象是否返回 '404 Not Found' ?

javascript - $state.go 没有改变 Url

android - 按钮、OpenGL 和渲染器 - Android

java - FileNotFoundException 从 SD 卡路径加载图像