Java 无法实例化公共类,Android studio说它不是公共类
我想在android中使用BluetoothA2DPSink服务, 它是一个隐藏类,但我构建了一个修改过的SDK和ROM,现在Android studio可以看到它了。 问题是我无法使用它,无论何时我尝试 'BluetoothA2DPSink接收器=新BluetoothA2DPSink' 我得到这个错误:BluetoothA2DPSink在“android.bluetooth.BluetoothA2DPSink”中不是公共的。不能从外包装进入。 我核实过了,事实上是公开的: 公共最终类BluetoothA2dpSink实现BluetoothProfile{。。。 我如何使用它的方法?Java 无法实例化公共类,Android studio说它不是公共类,java,android,Java,Android,我想在android中使用BluetoothA2DPSink服务, 它是一个隐藏类,但我构建了一个修改过的SDK和ROM,现在Android studio可以看到它了。 问题是我无法使用它,无论何时我尝试 'BluetoothA2DPSink接收器=新BluetoothA2DPSink' 我得到这个错误:BluetoothA2DPSink在“android.bluetooth.BluetoothA2DPSink”中不是公共的。不能从外包装进入。 我核实过了,事实上是公开的: 公共最终类Bluet
任何帮助都将不胜感激。如果您正确粘贴了错误消息,问题不在于类,而在于构造函数。请注意,BluetoothA2DPSink中的括号在“android.bluetooth.BluetoothA2DPSink”中不公开。不能从包外访问-这是对构造函数的引用,而不是类。请确保零参数构造函数是公共的。如果正确粘贴了错误消息,则问题不在于类,而在于构造函数。请注意,BluetoothA2DPSink中的括号在“android.bluetooth.BluetoothA2DPSink”中不是公共的。不能从包外访问-这是对构造函数的引用,而不是类的引用。Make确保零参数构造函数是公共的。请尝试以下代码。这是使用反射完成的。您不能简单地通过调用BluetoothA2DPSink的构造函数来创建对象。您还需要使用另一个类BluetoothProfile.java 对象mbluea2dpsink
/**
* This function will connect to A2DPsink profile of the server device to manage audio profile connection
*/
public void getBluetoothA2DPsink() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try {
final int A2DPprofile = BluetoothProfile.class.getField("A2DP_SINK").getInt(null);
BluetoothProfile.ServiceListener mProfileListener1 = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == A2DPprofile) {
mBluetoothA2DPSink = proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == A2DPprofile) {
mBluetoothA2DPSink = null;
try {
mContext.unregisterReceiver(mA2DPReciever);
} catch (IllegalArgumentException e) {
Log.e(TAG, e.getMessage());
}
}
}
};
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(mContext, mProfileListener1, A2DPprofile);
} catch (NoSuchFieldException | IllegalAccessException e) {
Log.e(TAG, e.getMessage());
}
}
尝试下面的代码。这是使用反射完成的。您不能简单地通过调用BluetoothA2DPSink的构造函数来创建对象。您还需要使用另一个类BluetoothProfile.java 对象mbluea2dpsink
/**
* This function will connect to A2DPsink profile of the server device to manage audio profile connection
*/
public void getBluetoothA2DPsink() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try {
final int A2DPprofile = BluetoothProfile.class.getField("A2DP_SINK").getInt(null);
BluetoothProfile.ServiceListener mProfileListener1 = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == A2DPprofile) {
mBluetoothA2DPSink = proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == A2DPprofile) {
mBluetoothA2DPSink = null;
try {
mContext.unregisterReceiver(mA2DPReciever);
} catch (IllegalArgumentException e) {
Log.e(TAG, e.getMessage());
}
}
}
};
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(mContext, mProfileListener1, A2DPprofile);
} catch (NoSuchFieldException | IllegalAccessException e) {
Log.e(TAG, e.getMessage());
}
}