Java Android setOnDragListener显示错误
我正在开发一个屏幕锁定的android应用程序。我已经使用了屏幕广播接收器。在我的应用程序中,我想使用拖放。因此,当我使用Java Android setOnDragListener显示错误,java,android,android-intent,ontouchlistener,android-broadcastreceiver,Java,Android,Android Intent,Ontouchlistener,Android Broadcastreceiver,我正在开发一个屏幕锁定的android应用程序。我已经使用了屏幕广播接收器。在我的应用程序中,我想使用拖放。因此,当我使用setOnDragListener时,它会显示一个错误 java.lang.RuntimeException:接收广播意图时出错{ act=init view flg=0x10}in com.app.lockscreenlibrary。LockBroadcastReceiver@f22ed7d. 并且库文件引用是 当我使用drop.setOnDragListener(Lock
setOnDragListener
时,它会显示一个错误
java.lang.RuntimeException:接收广播意图时出错{
act=init view flg=0x10}in
com.app.lockscreenlibrary。LockBroadcastReceiver@f22ed7d.
并且库文件引用是
当我使用drop.setOnDragListener(LockView.java文件中的new View.OnDragListener(){时,可能会发生错误。我想获取拖动的项值。(下面是我的项是btn0,拖动部分是bottomlinear)
我的代码是LockView.java:
public class LockView extends FrameLayout {
public LinearLayout drop;
TextView text, sucess;
int total, failure = 0;
ImageView viewDrop;
private GestureDetector gestureDetector;
private Context mContext;
Button btnUnlock;
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnH, btnS;
Button buttonB;
private int CLICK_ACTION_THRESHHOLD = 200;
private float startX;
private float startY;
int nums[] = new int[4];
int position = 0;
ImageView imageView1, imageView2, imageView3, imageView4;
public LockView(Context context) {
this(context, null);
}
public LockView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LockView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(final Context context) {
mContext = context;
View view = inflate(context, R.layout.activity_screen_lock, null);
gestureDetector = new GestureDetector(context, new SingleTapConfirm());
drop = (LinearLayout) findViewById(R.id.bottomlinear);
sucess = (TextView) findViewById(R.id.Sucess);
drop.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DROP:{
failure = failure+1;
return(true);
}
case DragEvent.ACTION_DRAG_ENDED:{
total = total +1;
int suc = total - failure;
sucess.setText("Sucessful Drops :"+suc);
text.setText("Total Drops: "+total);
return(true);
}
default:
break;
}
return true;
}
});
btnUnlock = (Button) view.findViewById(R.id.unlock);
btnUnlock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LockHelper.getLockLayer().unlock();
}
});
btn0 = (Button) view.findViewById(R.id.btn0);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
btn3 = (Button) view.findViewById(R.id.btn3);
btn4 = (Button) view.findViewById(R.id.btn4);
btn5 = (Button) view.findViewById(R.id.btn5);
btn6 = (Button) view.findViewById(R.id.btn6);
btn7 = (Button) view.findViewById(R.id.btn7);
btn8 = (Button) view.findViewById(R.id.btn8);
btn9 = (Button) view.findViewById(R.id.btn9);
btnH = (Button) view.findViewById(R.id.btnH);
btnS = (Button) view.findViewById(R.id.btnS);
imageView1 = (ImageView) view.findViewById(R.id.imageView);
imageView2 = (ImageView) view.findViewById(R.id.imageView2);
imageView3 = (ImageView) view.findViewById(R.id.imageView3);
imageView4 = (ImageView) view.findViewById(R.id.imageView4);
buttonB = (Button) view.findViewById(R.id.buttonB);
btn0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 0;
}
setPosition(position);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 2;
}
setPosition(position);
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 3;
}
setPosition(position);
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 4;
}
setPosition(position);
}
});
btn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 5;
}
setPosition(position);
}
});
btn6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 6;
}
setPosition(position);
}
});
btn7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 7;
}
setPosition(position);
}
});
btn8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 8;
}
setPosition(position);
}
});
btn9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position > -1 && position < 4) {
nums[position] = 9;
}
setPosition(position);
}
});
btn1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(btn1);
v.startDrag(data, shadow, v, 0);
Log.d("LOGTAG", "onTouch");
return true;
}
});
if (position == 3) {
checkSpeedDial();
} else if (position == 2) {
checkSpeedDial();
} else if (position == 1) {
checkSpeedDial();
} else if (position == 0) {
checkSpeedDial();
} else if (position == -1) {
checkSpeedDial();
}
buttonB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position == 1) {
imageView1.setImageResource(0);
position--;
Log.d("LOGTAG", "clicked1 : position =" + position);
} else if (position == 2) {
imageView2.setImageResource(0);
position--;
Log.d("LOGTAG", "clicked2 : position =" + position);
} else if (position == 3) {
imageView3.setImageResource(0);
position--;
Log.d("LOGTAG", "clicked3 : position =" + position);
} else if (position == 4) {
imageView4.setImageResource(0);
position--;
Log.d("LOGTAG", "clicked4 : position =" + position);
} else {
}
}
});
if (position > 3) {
checkPassword();
}
addView(view);
}
private void setPosition(int pos) {
if (pos == 0) {
imageView1.setImageResource(R.drawable.lock_circle);
position++;
} else if (pos == 1) {
imageView2.setImageResource(R.drawable.lock_circle);
position++;
} else if (pos == 2) {
imageView3.setImageResource(R.drawable.lock_circle);
position++;
} else if (pos == 3) {
imageView4.setImageResource(R.drawable.lock_circle);
position++;
checkPassword();
} else {
}
}
public void showLockHome() {
}
private void checkSpeedDial() {
Log.d("LOGTAG", position + " ::" + nums[0] + " 2: " + nums[1] + " 3: " + nums[2] + " 4: " + nums[3]);
}
public void checkPassword() {
Log.d("LOGTAG", "1 :" + nums[0] + " 2: " + nums[1] + " 3: " + nums[2] + " 4: " + nums[3]);
int pas[] = new int[4];
pas[0] = 1;
pas[1] = 1;
pas[2] = 1;
pas[3] = 1;
if (Arrays.equals(nums, pas)) {
LockHelper.getLockLayer().unlock();
} else {
Log.d("LOGTAG", "Wrong Password");
}
}
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d("LockView", "clicked1");
return true;
}
}
private boolean isAClick(float startX, float endX, float startY, float endY) {
float differenceX = Math.abs(startX - endX);
float differenceY = Math.abs(startY - endY);
if (differenceX > CLICK_ACTION_THRESHHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHHOLD) {
return false;
}
return true;
}
}
public类LockView扩展了FrameLayout{
公共线路布局下降;
文本查看文本,成功;
整数总计,故障=0;
ImageView视图下拉列表;
私人手势检测器;
私有上下文;
按钮BTUNLOCK;
按钮btn1、btn2、btn3、btn4、btn5、btn6、btn7、btn8、btn9、btn0、btnH、btnS;
按钮b;
私有int点击动作阈值=200;
私人浮动startX;
私人浮星;
int nums[]=新int[4];
int位置=0;
ImageView imageView1、imageView2、imageView3、imageView4;
公共锁视图(上下文){
这个(上下文,空);
}
public LockView(上下文、属性集属性){
这(上下文,属性,0);
}
public LockView(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
init(上下文);
}
私有void init(最终上下文){
mContext=上下文;
视图=充气(上下文,R.layout.activity\u screen\u lock,null);
gestureDetector=new gestureDetector(上下文,new SingleTapConfirm());
下降=(线性布局)findViewById(R.id.bottomlinear);
suces=(TextView)findViewById(R.id.suces);
drop.setOnDragListener(新视图.OnDragListener(){
@凌驾
公共布尔onDrag(视图v,DrageEvent事件){
final int action=event.getAction();
开关(动作){
案例DrageEvent.ACTION\u DRAG\u已启动:
打破
案例DrageEvent.ACTION\u DRAG\u退出:
打破
案例DrageEvent.ACTION\u DRAG\u输入:
打破
案例DrageEvent.ACTION_DROP:{
故障=故障+1;
返回(真);
}
案例DrageEvent.ACTION\u DRAG\u已结束:{
总计=总计+1;
int suc=总故障;
success.setText(“successful Drops:+suc”);
text.setText(“总落差:”+Total);
返回(真);
}
违约:
打破
}
返回true;
}
});
btnUnlock=(按钮)view.findviewbyd(R.id.unlock);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
LockHelper.getLockLayer().unlock();
}
});
btn0=(按钮)view.findViewById(R.id.btn0);
btn1=(按钮)view.findViewById(R.id.btn1);
btn2=(按钮)view.findViewById(R.id.btn2);
btn3=(按钮)view.findViewById(R.id.btn3);
btn4=(按钮)view.findViewById(R.id.btn4);
btn5=(按钮)view.findViewById(R.id.btn5);
btn6=(按钮)view.findViewById(R.id.btn6);
btn7=(按钮)view.findViewById(R.id.btn7);
btn8=(按钮)view.findViewById(R.id.btn8);
btn9=(按钮)view.findViewById(R.id.btn9);
btnH=(按钮)view.findviewbyd(R.id.btnH);
btnS=(按钮)view.findviewbyd(R.id.btnS);
imageView1=(ImageView)view.findViewById(R.id.ImageView);
imageView2=(ImageView)view.findViewById(R.id.imageView2);
imageView3=(ImageView)view.findViewById(R.id.imageView3);
imageView4=(ImageView)view.findViewById(R.id.imageView4);
buttonB=(Button)view.findViewById(R.id.buttonB);
btn0.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(位置>-1和位置<4){
nums[位置]=0;
}
设置位置(位置);
}
});
btn2.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(位置>-1和位置<4){
nums[位置]=2;
}
设置位置(位置);
}
});
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(位置>-1和位置<4){
nums[位置]=3;
}
设置位置(位置);
}
});
btn4.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(位置>-1和位置<4){
nums[位置]=4;
}
设置位置(位置);
}
});
btn5.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(位置>-1和位置<4){
nums[位置]=5;
}
设置位置(位置);
}
});
btn6.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(位置>-1和位置<4){
nums[位置]=6;
public enum LockHelper implements SwipeEvent {
INSTANCE;
private static Context mContext;
private final int UNLOCK = 830;
private final int UNLOCK_WITH_PASSWORD = 831;
private final int SWITCH_TO_GUEST = 345;
public static final String INIT_VIEW_FILTER = "init view";
public static final String START_SUPERVISE = "start supervise";
public static final String STOP_SUPERVISE = "stop supervise";
public static final String SHOW_SCREEN_LOCKER = "show screen locker";
private static LockView mLockView;
private static LockLayer mLockLayer;
public void initialize(Context context) {
initContextViewAndLayer(context);
loadLockView(context);
}
public static LockView getLockView() {
if (mLockView == null)
throw new NullPointerException("init first");
return mLockView;
}
public static LockLayer getLockLayer() {
if (mLockLayer == null)
throw new NullPointerException("init first");
return mLockLayer;
}
public void initLockViewInBackground(final Context context) {
if (context == null)
throw new NullPointerException("context == null, assign first");
if (mLockView == null || mLockLayer == null)
initContextViewAndLayer(context);
}
public void initContextViewAndLayer(Context context) {
if (mContext == null)
synchronized (this) {
if (mContext == null)
mContext = context;
}
//init layout view
if (mLockView == null)
synchronized (this) {
if (mLockView == null)
mLockView = new LockView(context);
}
if (mLockLayer == null)
synchronized (this) {
if (mLockLayer == null)
mLockLayer = LockLayer.getInstance(context, mLockView);
}
}
private volatile boolean mIsInitialized = false;
public void loadLockView(Context context) {
mLockView.showLockHome();
if( !mIsInitialized){
mIsInitialized = true;
}
mLockLayer.lock();
showLockLayer();
}
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UNLOCK:
unlock();
break;
case UNLOCK_WITH_PASSWORD:
if (!(msg.obj instanceof String)) break;
String password = (String) msg.obj;
switchUserIfExistOrAlertUser(password);
break;
default:
break;
}
}
};
private void unlock() {
mLockLayer.unlock();
mContext.sendBroadcast(new Intent(LockHelper.STOP_SUPERVISE));
mContext.sendBroadcast(new Intent(CoreIntent.ACTION_SCREEN_LOCKER_UNLOCK));
}
private void switchUserIfExistOrAlertUser(String password) {
if (TextUtils.isEmpty(password)) {
wrong();
return;
}
if (!password.equals("1234")) {
wrong();
return;
}
unlockScreenAndResetPinCode();
}
private void unlockScreenAndResetPinCode() {
unlock();
}
private void wrong() { }
public static final String INTENT_KEY_WITH_SECURE = "with_secure";
@Override
public <S, T> void onSwipe(S s, T t) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
LockHelper.INSTANCE.getLockLayer().removeLockView();
}
}, 1000);
}
private void triggerCameraWithSecure(Context context, boolean withSecure) { }
private void showLockLayer() {
mLockView.showLockHome();
mLockLayer.bringBackLockView();
}
public void vibrate(long milliseconds) {
if (mContext == null) return;
Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(milliseconds == 0 ? 500 : milliseconds);
}
}
final public class LockBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = LockBroadcastReceiver.class.getSimpleName();
private volatile boolean bInterruptSupervisor = false;
private ScheduledThreadPoolExecutor mExecutor;
private FutureRunnable mSupervisorRunnable;
private static final int SCHEDULE_TASK_NUMBER = 3;
private PhoneStateChange mPhoneStateChangeCallback;
public void assignPhoneStateChangeCallback(PhoneStateChange phoneStateChangeCallback) {
mPhoneStateChangeCallback = phoneStateChangeCallback;
}
@Override
public void onReceive(Context context, Intent intent) {
String mAction = intent.getAction();
switch (mAction) {
case LockHelper.INIT_VIEW_FILTER:
LockHelper.INSTANCE.initLockViewInBackground(context);
break;
case Intent.ACTION_SCREEN_ON:
refreshBatteryInfo();
bringLockViewBackTopIfNot();
break;
case CoreIntent.ACTION_SCREEN_LOCKER_UNLOCK:
shutdownScheduleExecutor();
break;
case LockHelper.START_SUPERVISE:
bInterruptSupervisor = false;
supervise(context.getApplicationContext());
break;
case LockHelper.STOP_SUPERVISE:
bInterruptSupervisor = true;
break;
case LockHelper.SHOW_SCREEN_LOCKER:
//DU.sd("broadcast", "locker received");
case Intent.ACTION_SCREEN_OFF:
LockHelper.INSTANCE.initialize(context);
LockHelper.INSTANCE.getLockLayer().lock();
bInterruptSupervisor = true;
break;
case Intent.ACTION_POWER_CONNECTED:
//LockHelper.INSTANCE.getLockView().batteryChargingAnim();
break;
case Intent.ACTION_POWER_DISCONNECTED:
//LockHelper.INSTANCE.getLockView().batteryChargingAnim();
break;
case Intent.ACTION_SHUTDOWN:
break;
case "android.intent.action.PHONE_STATE":
TelephonyManager tm =
(TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
mPhoneStateChangeCallback.ringing();
Log.i(TAG, "RINGING :" + intent.getStringExtra("incoming_number"));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
mPhoneStateChangeCallback.offHook();
//DU.sd(TAG, "off hook");
break;
case TelephonyManager.CALL_STATE_IDLE:
mPhoneStateChangeCallback.idle();
Log.i(TAG, "incoming IDLE");
break;
}
break;
default:
break;
}
}
abstract class FutureRunnable implements Runnable {
private Future<?> future;
public Future<?> getFuture() {
return future;
}
public void setFuture(Future<?> future) {
this.future = future;
}
}
public void supervise(final Context context) {
initScheduleExecutor();
if (mSupervisorRunnable == null) {
mSupervisorRunnable = new FutureRunnable() {
public void run() {
if (bInterruptSupervisor) getFuture().cancel(true);
boolean cameraRunning = false;
Camera _camera = null;
try {
_camera = Camera.open();
cameraRunning = _camera == null;
} catch (Exception e) {
cameraRunning = true;
} finally {
if (_camera != null) {
_camera.release();
getFuture().cancel(true);
context.sendBroadcast(new Intent(LockHelper.SHOW_SCREEN_LOCKER));
}
}
if (!cameraRunning)
context.sendBroadcast(new Intent(LockHelper.SHOW_SCREEN_LOCKER));
}
};
}
Future<?> future = mExecutor.scheduleAtFixedRate(mSupervisorRunnable, 2000, 500, TimeUnit.MILLISECONDS);
mSupervisorRunnable.setFuture(future);
}
private void bringLockViewBackTopIfNot() {
initScheduleExecutor();
mExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
LockHelper.INSTANCE.getLockLayer().requestFullScreen();
}
}, 1000, 1000, TimeUnit.MILLISECONDS);
}
private void refreshBatteryInfo() {
initScheduleExecutor();
mExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
//LockHelper.INSTANCE.getLockView().refreshBattery();
}
}, 2, 2, TimeUnit.MINUTES);
}
private void initScheduleExecutor() {
if (mExecutor == null) {
synchronized (this) {
if (mExecutor == null)
mExecutor = new ScheduledThreadPoolExecutor(SCHEDULE_TASK_NUMBER);
}
}
}
public synchronized void shutdownScheduleExecutor() {
if (mExecutor == null) return;
mExecutor.shutdown();
mExecutor = null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:patternview="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_screen_lock"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="1"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:minWidth="10dp"/>
<LinearLayout
android:id="@+id/bottomlinear"
android:gravity="center"
android:background="#00ffff"
android:orientation="vertical"
android:layout_marginTop="50dp"
android:layout_height="75dp"
android:layout_width="75dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Sucess"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
drop = (LinearLayout) view.findViewById(R.id.bottomlinear);
drop = (LinearLayout) findViewById(R.id.bottomlinear);