android已经将UI操作放入主线程,仍然报错

在activity里就没问题,到了自定义class里就不行了 ,弹框之后,点 取消按钮 就报错:Only the original thread that created a view hierarchy can touch its views.

package com.juhui.zc;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.app.Dialog;

import android.os.Bundle;

import android.os.Debug;

import android.os.Handler;

import android.os.HandlerThread;

import android.os.Looper;

import android.os.Message;

import android.util.DisplayMetrics;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.Window;

import android.view.WindowManager;

import android.widget.AdapterView;

import android.widget.LinearLayout;

import android.widget.ListView;

import android.widget.TextView;

import com.juhui.zc.util.DebugLogUtil;

import com.juhui.zc.util.UiThread;

import com.meihua.passivelock.fastble.BleManager;

import com.meihua.passivelock.fastble.data.BleDevice;

import java.util.List;

/**

* Created On 2018/7/23.

*/

public class BluetoothListDialog implements View.OnClickListener {

private Dialog dialog;

private Activity activity;

private List<BleDevice> scanResultList;

private BluetoothListAdapter adapter;

private ListView listDevice;

private TextView tvNoData;

private TextView tvTitle;

private TextView tvDisConnect;

private View viewDisConnect;

private TextView tvDismiss;

private BLEClickListener listener;

public BleDevice bleDevice;

// private Handler lockUiHandler;// 用于操作UI 的

// private UiThread lockThread;

public void setBleDevice(final BleDevice bleDevice) {

this.bleDevice = bleDevice;

// tvTitle.setText("已搜索到的设备");

if (bleDevice == null) {

tvDisConnect.setVisibility(View.GONE);

viewDisConnect.setVisibility(View.GONE);

} else {

tvDisConnect.setVisibility(View.VISIBLE);

viewDisConnect.setVisibility(View.VISIBLE);

}

}

public void setList(List<BleDevice> scanResultList1) {

this.scanResultList = scanResultList1;

if (adapter == null) {

adapter = new BluetoothListAdapter(activity);

}

adapter.setListData(scanResultList);

listDevice.setAdapter(adapter);

adapter.notifyDataSetChanged();

}

public interface BLEClickListener {

public void setOnItemClickListener(BleDevice bleDevice);

}

public BluetoothListDialog(Activity activity, BLEClickListener listener1) {

this.listener = listener1;

this.activity = activity;

dialog = new Dialog(activity);

dialog.setCanceledOnTouchOutside(false);

View contentView = LayoutInflater.from(activity).inflate(R.layout.act_bluetooth_list_dialog, null);

dialog.setContentView(contentView);

Window window = dialog.getWindow();

assert window != null;

window.setBackgroundDrawableResource(android.R.color.transparent);

LinearLayout linBackground = window.findViewById(R.id.linBackground);

WindowManager manager = activity.getWindow().getWindowManager();

DisplayMetrics outMetrics = new DisplayMetrics();

manager.getDefaultDisplay().getMetrics(outMetrics);

int width = outMetrics.widthPixels;

ViewGroup.LayoutParams lp = linBackground.getLayoutParams();

lp.width = (int) (width * 0.6);

linBackground.setLayoutParams(lp);

listDevice = window.findViewById(R.id.listDevice);

tvNoData = window.findViewById(R.id.tvNoData);

tvTitle = window.findViewById(R.id.tvTitle);

tvDisConnect = window.findViewById(R.id.tvDisConnect);

viewDisConnect = window.findViewById(R.id.viewDisConnect);

tvDismiss = window.findViewById(R.id.tvDismiss);

tvDismiss.setOnClickListener(this);

tvDisConnect.setOnClickListener(this);

listDevice.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

BleDevice bleDevice = scanResultList.get(i);

listener.setOnItemClickListener(bleDevice);

}

});

// initThread();

}

public void show() {

if (dialog != null) {

dialog.show();

}

}

public void dismiss() {

DebugLogUtil.getInstance().Debug((Looper.getMainLooper().getThread() == Thread.currentThread()) + "");

if (dialog != null && dialog.isShowing()) {

activity.runOnUiThread(new Runnable() {

@Override

public void run() {

dialog.dismiss();

}

});

}

}

@Override

public void onClick(View view) {

final int i = view.getId();

if (i == R.id.tvDismiss) {

dismiss();

} else if (i == R.id.tvDisConnect) {

if (bleDevice != null) {

BleManager.getInstance().disconnect(bleDevice);

}

dismiss();

}

// lockThread = new UiThread() {

// @Override

// public void run() {

// super.run();

// Message a = new Message();

// Bundle bundle = new Bundle();

// bundle.putInt("view", i);

// a.setData(bundle);

// a.what = 99;

//// lockUiHandler.sendEmptyMessage(i);

// lockUiHandler.sendMessage(a);

// }

// };

// lockThread.start();

}

}

回答

创建与引用时必须在同一线程,这个地方可能要调整

public BluetoothListDialog(Activity activity, BLEClickListener listener1) {

...

dialog = new Dialog(activity);

以上是 android已经将UI操作放入主线程,仍然报错 的全部内容, 来源链接: utcz.com/a/42523.html

回到顶部