gpt4 book ai didi

Android VpnService.Builder.establish() 偶尔为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:52 25 4
gpt4 key购买 nike

当我尝试为我的 VpnService 创建隧道接口(interface)时,出现以下错误:

Attempt to invoke virtual method 'java.lang.String android.os.ParcelFileDescriptor.toString()' on a null object reference

我目前唯一的解决办法是在发生这种情况时重启设备。否则,我根本无法创建隧道。

我创建隧道的代码:

// This is inside my VpnService implementation 

private ParcelFileDescriptor configureTunnelWithPushOpts(PushOpts popts)
{
VpnService.Builder builder = this.new Builder();

builder.setMtu ( currentServerPrefs.SERVER_MTU );
builder.addAddress ( popts.ip, 32 );
builder.addDnsServer ( popts.dns1 );
builder.addDnsServer ( popts.dns2 );
builder.addRoute ( "0.0.0.0", 0 );


// Note: Blocking mode is now enabled in native
// code under the setFileDescriptor function.
// builder.setBlocking(true);

builder.setConfigureIntent(
PendingIntent.getActivity(
this,
0,
new Intent(
this,
MainActivity.class
),
PendingIntent.FLAG_UPDATE_CURRENT)
);

final ParcelFileDescriptor vpnInterface;

synchronized (this) {
builder.setSession(currentServerPrefs.SERVER_ADDRESS);
vpnInterface = builder.establish();
}

Logger.info("New interface: " + vpnInterface.toString(), this);
return vpnInterface;
}

编辑:

根据文档,如果 VpnService 尚未准备好,establish 函数将返回 null,但是我在运行上述函数之前使用它来准备它.

// This is inside my MainActivity class

Intent intent = VpnService.prepare(getApplicationContext());
if (intent != null) {
startActivityForResult(intent, 0);
} else {
onActivityResult(0, RESULT_OK, null);
}

. . .

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(this, MyVpnService.class);
startService(intent);
}
}

我用来调试的手机

运行 Android 5.0.1 的 Google Nexus 5

Edit

I figured out how to replicate it so that it's no longer sporadic. Basically, if I uninstall the application while the VPN is connected, and then reinstall it and try to start it back up, I receive a null response when i run builder.establish(). I fear this could pose potential issues when the application is updated through the Google Play Store.

Aside from that, Please do not mark this question as a duplicate. Every other question on this matter has been given the answer that the service needs to be prepared before the builder is established, however I am preparing it in my case and have a different cause for my problem. Any help would be appreciated.

最佳答案

非常感谢您的提示,它让我找到了解决方案!

我在 Android 5.0.2 中遇到了同样的问题:一切正常,我安装了我正在开发的 vpn 应用程序。第二天,尽管正确调用了 prepare,但突然出现无法解析的 NullPointerException

可以通过以下步骤在我的环境中解决该问题:

  • 卸载应用程序。应用程序是否仍具有 VPN 功能并不重要。
  • 重启手机。确保它已完全关闭。
  • 重启后,确保不再安装该应用程序(我遇到了一些奇怪的开发问题,即在我的手机重启后重新安装了一个应用程序)。
  • 重新安装您的 VPN 应用。它现在应该接收一个 vpn 接口(interface),而不是 null

关于Android VpnService.Builder.establish() 偶尔为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45257457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com