Java 访问ArrayList时出现问题<;字符串>;从我的DialogFragment中的MainActivity
我需要能够从MainActivity.java类中的ArrayList(ScannedDevicesArrayList)传递ScannedDevicesListItemDialog.java类索引 我尝试在MainActivity.java类中创建一个函数(getScannedDeviceArrayList),用于返回ArrayList,然后在对话框的onClick中调用该函数。但是,在运行程序时,只要我单击对话框中的某个项目,应用程序就会崩溃 如何在对话框中引用MainActivity.java中的此ArrayList,以便能够对其执行一些简单的操作,例如在单击对话框中的项目时将其索引打印到控制台 MainActivity.javaJava 访问ArrayList时出现问题<;字符串>;从我的DialogFragment中的MainActivity,java,android,arraylist,crash,dialogfragment,Java,Android,Arraylist,Crash,Dialogfragment,我需要能够从MainActivity.java类中的ArrayList(ScannedDevicesArrayList)传递ScannedDevicesListItemDialog.java类索引 我尝试在MainActivity.java类中创建一个函数(getScannedDeviceArrayList),用于返回ArrayList,然后在对话框的onClick中调用该函数。但是,在运行程序时,只要我单击对话框中的某个项目,应用程序就会崩溃 如何在对话框中引用MainActivity.jav
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;
}
}
}
}
package com.august.customtisensortagclient;
导入android.Manifest;
导入android.annotation.SuppressLint;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.BluetoothManager;
导入android.bluetooth.le.BluetoothLeScanner;
导入android.bluetooth.le.ScanCallback;
导入android.bluetooth.le.ScanResult;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.os.AsyncTask;
导入android.support.v4.app.DialogFragment;
导入android.support.v4.app.Fragment;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.text.method.ScrollingMovementMethod;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
导入java.io.StringBufferInputStream;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
蓝牙经理;
蓝牙适配器mBluetoothAdapter;
蓝牙扫描仪;
文本视图控制台;
按钮清除按钮;
public ArrayList scannedDevicesArrayList=新建ArrayList();
int-scannedDevicesArrayListTop=-1;
私有最终静态整数请求\启用\ BT=1;
私有静态最终整数权限\请求\粗略\位置=1;
ArrayAdapter myArrayAdapter;
列表视图列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
console=(TextView)findViewById(R.id.console);
console.append(“控制台正在工作…\n”);
registerClearButton();
//功能代码
设置蓝牙();
开始扫描();
registerScannedDevicesListCallback();
//测试代码
}
公共void clearConsole(){
console.setText(“控制台正在工作…\n”);
ScannedDeviceArrayList.clear();
myArrayAdapter.notifyDataSetChanged();
扫描设备ArrayListTop=-1;
}
私有无效注册表ScannedDevicesListCallback(){
列表=(ListView)findViewById(R.id.myListView);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
TextView TextView=(TextView)视图;
DialogFragment newFragment=新扫描设备ListItemDialog();
show(getSupportFragmentManager(),“ScannedDevicesListItemDialog”);
}
});
}
私有无效注册表ClearButton(){
clear_按钮=(按钮)findViewById(R.id.clear_按钮);
清除\u按钮.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
clearConsole();
}
});
}
public void addDeviceToScannedDeviceList(){
列表=(ListView)findViewById(R.id.myListView);
myArrayAdapter=新的ArrayAdapter(此,
R.layout.my_list_view_items,scannedDevicesArrayList);
list.setAdapter(myArrayAdapter);
}
公共无效设置蓝牙(){
//获取蓝牙适配器
最终BluetoothManager BluetoothManager=
(BluetoothManager)getSystemService(Context.BLUETOOTH\u服务);
mBluetoothAdapter=bluetoothManager.getAdapter();
//获得蓝牙扫描
mBluetoothScanner=mBluetoothAdapter.getBluetoothLeScanner();
//确保蓝牙已启用
if(mBluetoothAdapter!=null&&!mBluetoothAdapter.isEnabled()){
Intent enableentent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用意图、请求和启用);
}
//确保位置已启用
if(this.checkSelfPermission(Manifest.permission.ACCESS\u\u LOCATION)!=PackageManager.permission\u已授予){
final AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“此应用程序需要位置访问”);
setMessage(“请授予位置访问权限,以便此应用程序可以检测外围设备。”);
setPositiveButton(android.R.string.ok,null);
builder.setOnDismissListener(新的DialogInterface.OnDismissListener(){
@凌驾
公共void onDismiss(对话框接口对话框){
requestPermissions(新字符串[]{Manifest.permission.ACCESS\u\u LOCATION},
权限(请求、粗略位置);
}
});
builder.show();
}
}
//设备扫描回调。
private ScanCallback leScanCallback=新建ScanCallback(){
@凌驾
公共void onScanResult(int callbackType、ScanResult){
而(!scannedDeviceArrayList.contains(result.getDevice().getAddress())&&(result.getRssi()>-55)){
ScannedDeviceArrayList.add(result.getDevice().getAd
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();
}
}
<?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>
<?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();
MainActivity mainActivity = (MainActivity) getActivity();