Iga*_*gal 11 methods android oncreate
当用户点击我的应用程序中的图标时,我希望应用程序首先检查设备是否已连接到互联网,然后根据收到的结果执行某些操作(因为知道它只是弹出一个对话框,通知设备是否是是否连接).所以我写了这段代码:
public class MainActivity extends Activity {
// SOME CONSTANTS WILL BE DEFINED HERE
AlertDialog.Builder builder = new AlertDialog.Builder(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.icoMyIcon).setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
if (isNetworkConnected()) {
builder.setMessage("Internet connected!").setCancelable(false)
.setPositiveButton("OK", null);
builder.create().show();
} else {
builder.setMessage("Internet isn\'t connected!")
.setCancelable(false)
.setPositiveButton("OK", null);
builder.create().show();
}
}
};
// Check if the device is connected to the Internet
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
当我试图在模拟器上运行此应用程序时,它一直在破碎,我在LogCat中收到此错误消息:
07-24 22:59:45.034: E/AndroidRuntime(894): FATAL EXCEPTION: main
07-24 22:59:45.034: E/AndroidRuntime(894): java.lang.RuntimeException: Unable to
instantiate activity ComponentInfo{com.my.app/com.my.app.MainActivity}:
java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.os.Looper.loop(Looper.java:123)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-24 22:59:45.034: E/AndroidRuntime(894): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 22:59:45.034: E/AndroidRuntime(894): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 22:59:45.034: E/AndroidRuntime(894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-24 22:59:45.034: E/AndroidRuntime(894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-24 22:59:45.034: E/AndroidRuntime(894): at dalvik.system.NativeStart.main(Native Method)
07-24 22:59:45.034: E/AndroidRuntime(894): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.Activity.getSystemService(Activity.java:3526)
07-24 22:59:45.034: E/AndroidRuntime(894): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
07-24 22:59:45.034: E/AndroidRuntime(894): at com.my.app.MainActivity.<init>(MainActivity.java:24)
07-24 22:59:45.034: E/AndroidRuntime(894): at java.lang.Class.newInstanceImpl(Native Method)
07-24 22:59:45.034: E/AndroidRuntime(894): at java.lang.Class.newInstance(Class.java:1429)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-24 22:59:45.034: E/AndroidRuntime(894): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-24 22:59:45.034: E/AndroidRuntime(894): ... 11 more
Run Code Online (Sandbox Code Playgroud)
为什么会这样,我该如何解决?我是新手,所以......请温柔!:)
Jon*_*lor 15
我认为这是因为你在创建之前实例化一个onClick监听器被调用.尝试在onCreate()方法中实例化onClick侦听器.
这可能也可能不是AlertDialog,但我不完全确定.
从技术上讲,我认为导致问题的是以下行:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Run Code Online (Sandbox Code Playgroud)
但是,因为这是isNetworkConnected()在onClick方法中调用的方法中调用的,所以移动onClick的实例化可以解决问题.
线索是在onCreate()之前的活动不可用的系统服务的例外
错误是由于创建此对象创建.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)
你应该在调用onCreate之后执行此操作.
| 归档时间: |
|
| 查看次数: |
37705 次 |
| 最近记录: |