当前位置: 代码迷 >> Android >> android判断联网,总是报错?
  详细解决方案

android判断联网,总是报错?

热度:87   发布时间:2016-04-28 06:01:09.0
android判断联网,总是报错?在线等,急!!
一直报错,错误在init()方法里,求解谢谢了!!!
public class MainActivity extends Activity {

private TextView text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

init();
}

/**
 * 初始化实现
 */
private void init() {
text = (TextView) findViewById(R.id.waytext);
// 获得系统级联网管理员对象
ConnectivityManager manager = (ConnectivityManager) this//这行的this是否正确
.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info == null) { // 无网情况下
// 跳转到网络设置页面
startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
} else { // 有网情况下
if (info.isAvailable()) { // 网络可用时
/** 是手机自带的联网方式 */
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
if (info.getState() == State.CONNECTED) {
text.setText(String.valueOf("手机网络可用并已连接" + "\n"
+ "连接网络方式为:\n" + info.getType() + ",MOBILE"));
}

/** WIFI联网方式 */
} else {
text.setText(String.valueOf("连接网络方式为:" + info.getType()
+ ",WI-FI"));
startActivity(new Intent(
android.provider.Settings.ACTION_WIFI_SETTINGS));
}
} else {
text.setText(String.valueOf("手机网络不可用"));
}

}
}
}
------解决方案--------------------
这个this应该没问题,你看一下logcat日志,哪行报错
------解决方案--------------------
看看是不是少了权限
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  相关解决方案