在获取应用程序时遇到问题我正在构建以连接到Internet。我已经建立了能够检查有网络并且它是一个wifi网络,然后它尝试连接到Internet并从RSS源获取数据。
我在安装在BlackBerry PlayBook和BlackBerry Z10上的Android播放器上运行该应用程序。 PlayBook的行为符合预期,它可以很好地获得Feed。如果Z10设法清除错误检查,它会抛出异常,但如果没有可用的网络,它会按预期运行。
我应该提一下,该应用程序在其Manifest文件中具有相关权限,但由于某种原因,当我通过Android Studio的调试运行程序并且adb通过时,只有PlayBook打扰我显示权限屏幕编程到BlackBerry ADB Proxy Manager。在大约三次调试之后,它也只是为此而烦恼。
完全不知道我接下来应该尝试什么。有人有任何想法吗?
编辑:如果它有帮助,这是我在onCreate方法中从Android Studio添加到示例FullScreen Activity应用程序的代码:
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
Boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting(); // Check for the existence of a network connection
String helloString;
if (isConnected == true) {
// There is a network connection
Log.i("helloHeadlines", "FullscreenActivity.onCreate - there is network available");
//helloString = headlines_feed.connectionStatus();
Boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI ;
if (isWiFi == true) {
// There is WiFi - we may try to use the Internet
headlines headlinesS = new headlines();
try {
helloString = headlinesS.headlines_stream()[0][0];
} catch (Exception e) {
helloString = "Error: Received an exception";
}
//helloString = "There is WiFi available";
} else {
helloString = "There is no WiFi available";
}
} else {
// There is not a network connection
helloString = "There is no network connection";
}
final TextView mTextView = (TextView) findViewById(R.id.fullscreen_content);
mTextView.setText(helloString);
我认为问题不在于我的标题类,因为当我将它运行到控制台窗口时,它在PlayBook和Eclipse上运行良好。
编辑2:想想我可能找到了问题的根源。更改了一行代码并收到了一条新的错误消息:
} catch (Exception e) {
helloString = "Error: Received an exception";
}
成了
} catch (Exception e) {
helloString = e.toString();
}
给出了结果
android.os.NetworkOnMainThreadException
答案 0 :(得分:0)
解决了它。去寻找异常,并在这个帖子中找到答案:networkonmainthread。不得不做一些重做,因为新线程没有正确地将其结果推入我的TextView。结束以下代码:
private String helloString;
private void setText() {
final TextView mTextView = (TextView) findViewById(R.id.fullscreen_content);
Log.i("helloHeadlines", "FullscreenActivity.onCreate - helloString equals " + helloString);
mTextView.setText(helloString);
}
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
headlines headlinesS = new headlines();
try {
helloString = headlinesS.headlines_stream()[0][0];
Log.i("helloHeadlines", "FullscreenActivity.thread.run - set helloString to " + helloString);
} catch (Exception e) {
helloString = e.toString();
Log.i("helloHeadlines", "FullscreenActivity.thread.run - " + e.toString());
}
Log.i("helloHeadlines", "FullscreenActivity.thread.run - exiting try loop");
setText();
}
});
以及onCreate方法中的以下内容:
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
Boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting(); // Check for the existence of a network connection
if (isConnected == true) {
// There is a network connection
Log.i("helloHeadlines", "FullscreenActivity.onCreate - there is network available");
//helloString = headlines_feed.connectionStatus();
Boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI ;
if (isWiFi == true) {
// There is WiFi - we may try to use the Internet
thread.start();
Log.i("helloHeadlines", "FullscreenActivity.onCreate - exited thread.");
//helloString = "There is WiFi available";
} else {
helloString = "There is no WiFi available";
}
} else {
// There is not a network connection
helloString = "There is no network connection";
}
setText();