我正在使用下面的代码构建一个浮动的Android应用程序。 但应用程序不断崩溃,我不知道为什么。有人可以告诉我这是什么问题吗?
public class FloatingApp extends Service {
private WindowManager windowManager;
private View chatHead;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
chatHead = inflater.inflate(R.layout.page, null, false);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
try {
chatHead.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams paramsF = params;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Get current time in nano seconds.
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, paramsF);
break;
}
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
}
顺便说一句,没有错误。我编辑了一些以前用于浮动图像的代码,我将其更改为在窗口管理器中显示布局。
来自LogCat:
06-28 04:13:39.145:D / AndroidRuntime(2377):关闭VM 06-28 04:13:39.149:W / dalvikvm(2377):threadid = 1:线程退出 未捕获的异常(组= 0xa4b8c648)06-28 04:13:39.297: E / AndroidRuntime(2377):致命异常:主06-28 04:13:39.297: E / AndroidRuntime(2377):java.lang.IllegalStateException:不能 执行活动方法06-28 04:13:39.297: E / AndroidRuntime(2377):at android.view.View $ 1.onClick(View.java:3633)06-28 04:13:39.297: E / AndroidRuntime(2377):at android.view.View.performClick(View.java:4240)06-28 04:13:39.297: E / AndroidRuntime(2377):at android.view.View $ PerformClick.run(View.java:17721)06-28 04:13:39.297:E / AndroidRuntime(2377):at android.os.Handler.handleCallback(Handler.java:730)06-28 04:13:39.297:E / AndroidRuntime(2377):at android.os.Handler.dispatchMessage(Handler.java:92)06-28 04:13:39.297:E / AndroidRuntime(2377):at android.os.Looper.loop(Looper.java:137)06-28 04:13:39.297: E / AndroidRuntime(2377):at android.app.ActivityThread.main(ActivityThread.java:5103)06-28 04:13:39.297:E / AndroidRuntime(2377):at java.lang.reflect.Method.invokeNative(Native Method)06-28 04:13:39.297:E / AndroidRuntime(2377):at java.lang.reflect.Method.invoke(Method.java:525)06-28 04:13:39.297: E / AndroidRuntime(2377):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 06-28 04:13:39.297:E / AndroidRuntime(2377):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)06-28 04:13:39.297:E / AndroidRuntime(2377):at dalvik.system.NativeStart.main(Native Method)06-28 04:13:39.297: E / AndroidRuntime(2377):引起: java.lang.reflect.InvocationTargetException 06-28 04:13:39.297: E / AndroidRuntime(2377):at java.lang.reflect.Method.invokeNative(Native Method)06-28 04:13:39.297:E / AndroidRuntime(2377):at java.lang.reflect.Method.invoke(Method.java:525)06-28 04:13:39.297: E / AndroidRuntime(2377):at android.view.View $ 1.onClick(View.java:3628)06-28 04:13:39.297: E / AndroidRuntime(2377):... 11更多06-28 04:13:39.297: E / AndroidRuntime(2377):引起: android.content.ActivityNotFoundException:无法找到显式 活动类{com.firsttry / com.firsttry.MainActivity2};有你 在AndroidManifest.xml中声明了此活动? 06-28 04:13:39.297:E / AndroidRuntime(2377):at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 06-28 04:13:39.297:E / AndroidRuntime(2377):at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 06-28 04:13:39.297:E / AndroidRuntime(2377):at android.app.Activity.startActivityForResult(Activity.java:3390)06-28 04:13:39.297:E / AndroidRuntime(2377):at android.app.Activity.startActivityForResult(Activity.java:3351)06-28 04:13:39.297:E / AndroidRuntime(2377):at android.app.Activity.startActivity(Activity.java:3587)06-28 04:13:39.297:E / AndroidRuntime(2377):at android.app.Activity.startActivity(Activity.java:3555)06-28 04:13:39.297:E / AndroidRuntime(2377):at com.firsttry.opening.moveto3(opening.java:37)06-28 04:13:39.297: E / AndroidRuntime(2377):... 14更多06-28 04:13:46.337: I /处理(2377):发送信号。 PID:2377 SIG:9
答案 0 :(得分:3)
“您是否在AndroidManifest.xml中声明了此活动?”
答案 1 :(得分:1)
请仔细看看这一行
引起:android.content.ActivityNotFoundException:无法找到显式活动类{com.firsttry / com.firsttry.MainActivity2};你有没有在AndroidManifest.xml中声明这个活动?
它表示您尚未在清单文件中声明活动
您需要在清单
中的应用程序标记内声明活动