我有一项非活动,使用异步任务检查互联网访问权限。顺便说一句,互联网访问检查的代码在活动中正常工作。现在,当我将代码添加到非活动类时,我希望将响应保存到字符串中并使用“startActivityForResult”发送到MainActivity。我添加了一个Toast来检查值是否传递给MainActivity,但是Toast显示为空白。因此,响应不是保存字符串或不发送到MainActivity。我也发布了日志猫,就像我在MainActivity中单击按钮一样,因为值为null,应用程序崩溃了。
LogCat
09-26 13:31:26.872: E/AndroidRuntime(22041): FATAL EXCEPTION: main
09-26 13:31:26.872: E/AndroidRuntime(22041): Process: com.example.networkping, PID: 22041
09-26 13:31:26.872: E/AndroidRuntime(22041): java.lang.NullPointerException
09-26 13:31:26.872: E/AndroidRuntime(22041): at com.example.networkping.MainActivity$1.onClick(MainActivity.java:43)
09-26 13:31:26.872: E/AndroidRuntime(22041): at android.view.View.performClick(View.java:4562)
09-26 13:31:26.872: E/AndroidRuntime(22041): at android.view.View$PerformClick.run(View.java:18918)
09-26 13:31:26.872: E/AndroidRuntime(22041): at android.os.Handler.handleCallback(Handler.java:808)
09-26 13:31:26.872: E/AndroidRuntime(22041): at android.os.Handler.dispatchMessage(Handler.java:103)
09-26 13:31:26.872: E/AndroidRuntime(22041): at android.os.Looper.loop(Looper.java:193)
09-26 13:31:26.872: E/AndroidRuntime(22041): at android.app.ActivityThread.main(ActivityThread.java:5388)
09-26 13:31:26.872: E/AndroidRuntime(22041): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 13:31:26.872: E/AndroidRuntime(22041): at java.lang.reflect.Method.invoke(Method.java:515)
09-26 13:31:26.872: E/AndroidRuntime(22041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-26 13:31:26.872: E/AndroidRuntime(22041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
09-26 13:31:26.872: E/AndroidRuntime(22041): at dalvik.system.NativeStart.main(Native Method)
Non-Activity AsyncTask class
public class Internet extends AsyncTask<Void, Void, Boolean>{
String sam;
Context context;
Activity activity;
public Internet(Activity ac) {
activity = ac;
}
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
protected void onPostExecute(Boolean result){
if(result){
// Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
sam="1";
Intent view = new Intent(activity, MainActivity.class);
view.putExtra("zipcode", sam.toString());
activity.startActivityForResult(view, 1);
}
else{
// Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
sam="0";
Intent view = new Intent(activity, MainActivity.class);
view.putExtra("zipcode", sam.toString());
activity.startActivityForResult(view, 1);
}
}
@Override
protected Boolean doInBackground(Void... params) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL( "http://www.google.com" ).openConnection());
urlc.connect();
int resp = urlc.getResponseCode();
return resp == 200;
} catch (IOException e) {
//Log.e(LOG_TAG, "Error checking internet connection", e);
System.out.println("Error checking internet connection"+e);
}
} else {
//Log.d(LOG_TAG, "No network available!");
System.out.println("No network available!");
}
return false;
}
}
MainActivity
public class MainActivity extends Activity {
Button check;
String zipcode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zipcode = getIntent().getExtras().getString("zipcode");
Toast.makeText(getApplicationContext(), zipcode, 3000).show();
check=(Button)findViewById(R.id.check);
check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//new CheckInternetConnection().execute();
//String zipcode = getIntent().getExtras().getString("zipcode");
if(zipcode.equals("1")){
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}else if(zipcode.equals("0")){
Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
}
}
});
}// oncreate
} //activity
答案 0 :(得分:0)
尝试使用
zipcode = getIntent().getStringExtra("zipcode");
而不是
zipcode = getIntent().getExtras().getString("zipcode");
答案 1 :(得分:0)
实现此目标最简单有效的方法是使用事件。在android中,像大多数其他开发环境一样,你总是希望避免在你的代码中耦合 - 这意味着你需要尽可能多的松耦合,以避免你的部分类对其他人有太多的了解。
因此,使用https://github.com/greenrobot/EventBus库(非常受欢迎和推荐),只要活动订阅该活动,您就可以通过发布新活动来简单地通知活动互联网连接状态。
然后,您只需覆盖 onEvent(EventClassName事件)方法,相应地设置字符串变量。
我希望这会有所帮助 - 有关如何使用上述事件库的文档对如何开始使用非常有帮助。
祝你好运!