当我试图为我的VpnService创建隧道接口时,我会得到以下错误:
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;
}编辑:
根据文档,如果还没有准备好establish函数,那么null函数将返回VpnService,但是在运行上面的函数之前,我将使用它来准备。
// 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);
}
}用于调试的手机
Google Nexus 5 Running Android 5.0.1
编辑 我想出了如何复制它这样它就不再是零星的了。基本上,如果我在连接VPN时卸载应用程序,然后重新安装它并尝试重新启动它,则在运行
builder.establish()时收到一个空响应。当应用程序通过Google更新时,我担心这可能会带来潜在的问题。 除此之外,请不要将此问题标记为重复。有关这件事的每一个其他问题都得到了答复,即在建造者建立之前,服务需要准备好,但我正在为我的情况做准备,对我的问题有不同的原因。任何帮助都将不胜感激。
发布于 2018-04-14 16:34:37
非常感谢你的暗示,它让我找到了解决方案!
我在Android5.0.2中也遇到了同样的问题:一切正常,我安装了我正在开发的vpn应用程序。第二天,突然间,一个无法解决的NullPointerException被正确地调用了prepare。
这个问题可以通过以下步骤在我的环境中得到解决:
null。https://stackoverflow.com/questions/45257457
复制相似问题