gpt4 book ai didi

java - 从我的DialogFragment中的MainActivity访问ArrayList 遇到麻烦

转载 作者:行者123 更新时间:2023-12-03 17:45:14 25 4
gpt4 key购买 nike

我需要能够从MainActivity.java类中的ArrayList(ScannedDevicesArrayList)中传递ScannedDevicesListItemDialog.java类索引。

我尝试在MainActivity.java类中创建一个函数(getScannedDevicesArrayList)以返回ArrayList,然后在对话框的onClick中调用该函数。但是,在运行程序时,只要单击对话框中的某个项目,应用程序就会崩溃。

如何在对话框中从MainActivity.java引用此ArrayList以便能够执行简单的操作,例如在单击对话框中的项目时将其索引打印到控制台?

MainActivity.java

package com.august.customtisensortagclient;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


BluetoothManager mBluetoothManager;
BluetoothAdapter mBluetoothAdapter;
BluetoothLeScanner mBluetoothScanner;
TextView console;
Button clear_button;
public ArrayList<String> scannedDevicesArrayList = new ArrayList<String>();
int scannedDevicesArrayListTop = -1;
private final static int REQUEST_ENABLE_BT = 1;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
ArrayAdapter<String> myArrayAdapter;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
console = (TextView) findViewById(R.id.Console);
console.append("Console is working...\n");
registerClearButton();




// Functional code

setupBluetooth();
startScanning();
registerScannedDevicesListCallback();

// Test code


}

public void clearConsole(){
console.setText("Console is working...\n");
scannedDevicesArrayList.clear();
myArrayAdapter.notifyDataSetChanged();
scannedDevicesArrayListTop = -1;
}

private void registerScannedDevicesListCallback() {
list = (ListView) findViewById(R.id.myListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
DialogFragment newFragment = new ScannedDevicesListItemDialog();
newFragment.show(getSupportFragmentManager(), "ScannedDevicesListItemDialog");

}
});
}

private void registerClearButton(){
clear_button = (Button) findViewById(R.id.clear_button);
clear_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clearConsole();
}
});
}

public void addDeviceToScannedDevicesList() {
list = (ListView) findViewById(R.id.myListView);
myArrayAdapter = new ArrayAdapter<String>(this,
R.layout.my_list_view_items, scannedDevicesArrayList);
list.setAdapter(myArrayAdapter);
}

public void setupBluetooth() {
// Get BluetoothAdapter
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Get BluetoothLeScanner
mBluetoothScanner = mBluetoothAdapter.getBluetoothLeScanner();
// Make sure Bluetooth is enabled
if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
// Make sure location is enabled
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect peripherals.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}

// Device scan callback.
private ScanCallback leScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
while (!scannedDevicesArrayList.contains(result.getDevice().getAddress()) && (result.getRssi() > -55)){
scannedDevicesArrayList.add(result.getDevice().getAddress());
scannedDevicesArrayListTop++;
addDeviceToScannedDevicesList();
console.append(Integer.toString(result.getRssi()));
}
}
};

public ArrayList<String> getScannedDevicesArrayList(){
return scannedDevicesArrayList;
}

public int getScannedDevicesArrayListTop() {
return scannedDevicesArrayListTop;
}

public void startScanning() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
mBluetoothScanner.startScan(leScanCallback);
}
});
}

public void stopScanning() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
mBluetoothScanner.stopScan(leScanCallback);
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
System.out.println("coarse location permission granted");
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since location access has not been granted, " +
"this app will not be able to discover beacons when in the background.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
}

});
builder.show();
}
return;
}
}
}


}

ScannedDevicesListItemDialog.java
package com.august.customtisensortagclient;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;


public class ScannedDevicesListItemDialog extends DialogFragment {


String[] options = {"Left", "Right"};


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Connect as Left or Right?")
.setItems(options, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MainActivity mainActivity = new MainActivity();
ArrayList<String> thisScannedDevicesArrayList = mainActivity.getScannedDevicesArrayList();
((MainActivity)getActivity()).console.setText("Worked");
((MainActivity)getActivity()).console.setText(thisScannedDevicesArrayList.get(0));
}
});
return builder.create();
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="0dp">

<TextView
android:id="@+id/Console"
android:layout_width="272dp"
android:layout_height="46dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="200dp" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter"
app:srcCompat="@mipmap/gruneisen_logo" />

<Button
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/Console"
android:layout_marginBottom="131dp"
android:text="CLEAR" />

<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="379dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

my_list_view_items.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="44sp">

</TextView>

最佳答案

MainActivity mainActivity = new MainActivity();

不是您访问 Activity 的方式。这也不是您访问已创建的类的方式,但这是另一个问题。

使用该代码,您将创建MainActivity的新实例,而不会获得当前存在的实例。这意味着您的ArrayList为null,因为它从未初始化。

要访问您的列表,请使用 getActivity()函数,该函数将获取Fragment的父Activity的实例,并将其转换为MainActivity:
MainActivity mainActivity = (MainActivity) getActivity();

关于java - 从我的DialogFragment中的MainActivity访问ArrayList <String>遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52338568/

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