正在获取静态类的android视图



我一直在尝试在我的android应用程序中创建一个类,以便能够发布小吃条。这个类是一个蓝牙连接的管理器,我需要在我的主要活动中使它保持静态才能实现这一点。由于这个原因,我无法将android上下文类发送到它或存储在中,这使我无法获得制作小吃条所需的视图。在整个蓝牙服务类中,我使用runOnUiThread(((->snackbarMsg尝试显示小吃条。当我将视图作为构造函数的参数发送时,它曾经工作过,但这只是第一次显示主屏幕,如果我切换活动并返回,它将停止工作,这也导致了内存泄漏。有其他方法可以解决这个问题吗?感谢您的帮助。

目前主要活动中的代码如下:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
//Initialise some static variables needed across the whole program
private static BluetoothService bluetoothService;
private static final String TAG = MainActivity.class.getSimpleName();
protected static ArrayList<LocationData> locations = new ArrayList<>();
protected static List<String> categories = new ArrayList<>();
Spinner spinner;
private static boolean doneFirstRun = false;
protected static LocationData selectedLocation = new LocationData();
MainActivity instance = this;
public MainActivity() {
}
/*
TODO: view in bluetooth service causes memory leak
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
if (!doneFirstRun){
//Getting storage
readFromFile(MainActivity.this);
//adding example location
if (locations.size()==0){
LocationData spaceRay = new LocationData();
spaceRay.setName("SpaceRay");
spaceRay.setLatitude(59.40384);
spaceRay.setLongitude(17.95228);
spaceRay.setInclination(-85);
spaceRay.setDirection(200);
spaceRay.setAltitude(99990);
locations.add(spaceRay);
}
//Checking and asking for relevant permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
@SuppressLint("InlinedApi") String[] permissions = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN};
ActivityCompat.requestPermissions(instance, permissions, 1);
}
bluetoothService = new BluetoothService();
//starting bluetooth networking activity on new thread
startBluetoothService();
doneFirstRun = true;
}
} catch (Exception exception){
Log.e(TAG, "Failed to do first run through of code: ", exception);
}

protected void startBluetoothService(){
try {
Log.i(TAG, "New thread started");
bluetoothService.run(MainActivity.this);
} catch (Exception e) {
Log.e(TAG, "Bluetooth service failed: ", e);
snackbarMsg("Bluetooth service failed");
}
}

像这样的是bluetooth_service类:

public class BluetoothService extends AppCompatActivity{
private static final String TAG = BluetoothService.class.getSimpleName();
private OutputStream outputStream;
private InputStream inputStream;
private BluetoothSocket socket;
private boolean writing = false;
public BluetoothService(){
}//constructor

//method to show snackbar message at the bottom of the screen
public Runnable snackbarMsg (String msg){
try {
View view = findViewById(R.id.btn_connect);
Snackbar snackbar = Snackbar.make(view, msg, BaseTransientBottomBar.LENGTH_SHORT);
snackbar.show();
} catch (Exception exception){
Log.e(TAG, "Could not show snackbar", exception);
}
return null;
}

错误消息如下:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:190)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:809)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:819)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:261)
at antennalocator.util.BluetoothService.snackbarMsg(BluetoothService.java:204)
at antennalocator.util.BluetoothService.lambda$write$4$antennalocator-util-BluetoothService(BluetoothService.java:159)
at antennalocator.util.BluetoothService$$ExternalSyntheticLambda5.run(Unknown Source:4)
at java.lang.Thread.run(Thread.java:1012)

设法解决了这个问题。我解决这个问题的方法是将视图作为参数发送到每个需要发布小吃条的方法中:例如write方法:

public void write(String s, View view) {
if (!writing){
new Thread(() -> {
try {
writing = true;
outputStream.write(s.getBytes());
runOnUiThread(snackbarMsg("Sent data", view));
lockout(3000);
writing = false;
} catch (Exception exception) {
writing = false;
Log.e(TAG, "Error occurred when sending data, restarting bluetooth service", exception);
runOnUiThread(() -> snackbarMsg("Could not send data, restarting bluetooth service", view));
disconnect();
}
}).start();
} else{
runOnUiThread(snackbarMsg("Please wait a bit before sending data", view));
}
}

主要:

bluetoothService.write(ConvertToString(selectedLocation), findViewById(R.id.btn_connect));

相关内容

  • 没有找到相关文章

最新更新