我正在编写一个Android应用程序,我想在调用浏览器显示流之前确保无线已连接,但是当单击按钮说明应用程序已停止运行时,它会关闭。我在android清单文件中设置了权限。这是我的代码。在一个名为Networking的单独类中,我这样做。
public class Networking extends Main {
public boolean isWirelessConnected()
{
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mwifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mwifi == null)
return false;
else
return true;
}
public void messageDialog()
{
AlertDialog.Builder alrtBuild = new AlertDialog.Builder(this);
alrtBuild
.setTitle("Wireless")
.setMessage("You are not Connected would you like to Connect to wireless?")
.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
WifiManager wifiManger = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManger.setWifiEnabled(true);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = alrtBuild.create();
alert.show();
}
}
在主要课程中我有
Networking netwrk = new Networking();
if(netwrk.wireless().isConnected())
{
Intent brwoserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.own3d.tv/HotshotGG"));
startActivity(brwoserIntent);
}
这是按下按钮时程序抛出的错误
12-08 19:51:16.904: E/AndroidRuntime(1549): FATAL EXCEPTION: main
12-08 19:51:16.904: E/AndroidRuntime(1549): java.lang.IllegalStateException: Could not execute method of the activity
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.view.View$1.onClick(View.java:3597)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.view.View.performClick(View.java:4202)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.view.View$PerformClick.run(View.java:17340)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.os.Handler.handleCallback(Handler.java:725)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.os.Looper.loop(Looper.java:137)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-08 19:51:16.904: E/AndroidRuntime(1549): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 19:51:16.904: E/AndroidRuntime(1549): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 19:51:16.904: E/AndroidRuntime(1549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-08 19:51:16.904: E/AndroidRuntime(1549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-08 19:51:16.904: E/AndroidRuntime(1549): at dalvik.system.NativeStart.main(Native Method)
12-08 19:51:16.904: E/AndroidRuntime(1549): Caused by: java.lang.reflect.InvocationTargetException
12-08 19:51:16.904: E/AndroidRuntime(1549): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 19:51:16.904: E/AndroidRuntime(1549): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.view.View$1.onClick(View.java:3592)
12-08 19:51:16.904: E/AndroidRuntime(1549): ... 11 more
12-08 19:51:16.904: E/AndroidRuntime(1549): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-08 19:51:16.904: E/AndroidRuntime(1549): at android.app.Activity.getSystemService(Activity.java:4463)
12-08 19:51:16.904: E/AndroidRuntime(1549): at nc.prog1415.Networking.isWirelessConnected(Networking.java:14)
12-08 19:51:16.904: E/AndroidRuntime(1549): at nc.prog1415.Main.hotshotBtns(Main.java:109)
12-08 19:51:16.904: E/AndroidRuntime(1549): ... 14 more
答案 0 :(得分:0)
很可能netwrk.wireless()
返回null并导致NullPointerException
。
我会将您的wireless()
方法更改为isWirelessConnected()
,如下所示
public boolean isWirelessConnected() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mwifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if ( mwifi == null )
return false;
else
return mwifi.isConnected();
}
然后在主课堂上,只需做
Networking netwrk = new Networking();
if( netwrk.isWirelessConnected() ) {
Intent brwoserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.own3d.tv/HotshotGG"));
startActivity(brwoserIntent);
}
答案 1 :(得分:0)
来自LogCat:
之前的活动无法使用的系统服务
onCreate()
此错误相当不言自明,因此请将代码更改为:
ConnectivityManager cm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
...
}
<强>加成强>
您的网络扩展了Main,我认为它是一个Activity。既然你不想要一个完整的Activity来做你正在做的事情,你应该采取不同的策略。您应该在构造函数中传递Networking an Context:
public class Networking {
Context context;
public Networking(Context con) {
context = con;
}
public boolean isWirelessConnected()
{
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mwifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mwifi == null)
return false;
else
return mwifi.isConnectedOrConnecting();
}
// etc
}
请注意,我在切换到context.getSystemService()
时也需要在创建对话框时执行此操作。
现在改变使用此方法的方式:
Networking netwrk = new Networking(this);
if(netwrk.isWirelessConnected())