无法在服务类

时间:2015-12-08 09:28:47

标签: android

我试图在" addNoteImageView"中添加视图。单击,如以下代码中所示。但是在这样做的时候,我得到InflateException.Logcat说你需要使用" Theme.appcompat"删除我已经使用的这个错误,但仍然错误不会消失。

我正在使用的代码

public class ServiceFloating extends Service {

private WindowManager windowManager;
private ImageView addNoteImageView;
private PopupWindow popupWindow;
boolean mHasDoubleClicked = false;
long lastPressTime;
private Boolean _enable = true;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    addNoteImageView = new ImageView(this);
    addNoteImageView.setImageResource(R.drawable.money_tracker_floating_icon);

    final WindowManager.LayoutParams param = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    param.gravity = Gravity.TOP | Gravity.LEFT;
    param.x = 0;
    param.y = 100;
    windowManager.addView(addNoteImageView, param);
    addNoteImageView.setOnTouchListener(new View.OnTouchListener() {
        private WindowManager.LayoutParams mParams = param;
        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:
                    long pressTime = System.currentTimeMillis();
                    //if double click
                    if (pressTime - lastPressTime <= 300) {
                        ServiceFloating.this.stopSelf();
                        mHasDoubleClicked = true;
                    } else
                        mHasDoubleClicked = false;
                    lastPressTime = pressTime;
                    initialX = mParams.x;
                    initialY = mParams.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mParams.x = initialX + (int) (event.getRawX() - initialTouchX);
                    mParams.y = initialY + (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(addNoteImageView, mParams);
                    break;
            }
            return false;
        }
    });
    addNoteImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            initiatePopupWindow(v);
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (addNoteImageView != null) {
        windowManager.removeView(addNoteImageView);
        disablePrefForFloatingIcon(true);
    }
}

//other method

public void initiatePopupWindow(View v) {
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
           LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.pop_up_layout, null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    popupWindow.showAsDropDown(v, 100, 100);
}

public void disablePrefForFloatingIcon(boolean shouldDisable) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ServiceFloating.this);
    SharedPreferences.Editor editor = preferences.edit();
    if (shouldDisable) {
        editor.putBoolean(Constant.PREF_SHOULD_ALLOW, false);
    } else {
        editor.putBoolean(Constant.PREF_SHOULD_ALLOW, true);
        editor.commit();
    }
}

我收到错误

   FATAL EXCEPTION: main
                                                                                                        Process: com.example.attriaman2520.simplestmoneytracker, PID: 10112
android.view.InflateException: Binary XML file line #19: Error inflating class android.support.design.widget.TextInputLayout  
                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:633)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
  at com.example.attriaman2520.simplestmoneytracker.ServiceFloating.initiatePopupWindow(ServiceFloating.java:114)                                                                                                         at com.example.attriaman2520.simplestmoneytracker.ServiceFloating$2.onClick(ServiceFloating.java:93)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
 at com.example.attriaman2520.simplestmoneytracker.ServiceFloating.initiatePopupWindow(ServiceFloating.java:114) 
at com.example.attriaman2520.simplestmoneytracker.ServiceFloating$2.onClick(ServiceFloating.java:93) 
    at android.view.View.performClick(View.java:4780) 
    at android.view.View$PerformClick.run(View.java:19866) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
    at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:103)
    at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:96)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
   at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at com.example.attriaman2520.simplestmoneytracker.ServiceFloating.initiatePopupWindow(ServiceFloating.java:114) 
                                                                                                        at com.example.attriaman2520.simplestmoneytracker.ServiceFloating$2.onClick(ServiceFloating.java:93) 
                                                                                                        at android.view.View.performClick(View.java:4780) 
                                                                                                        at android.view.View$PerformClick.run(View.java:19866) 
                                                                                                        at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

提前致谢。

1 个答案:

答案 0 :(得分:0)

Selvan的评论是正确的,只是解决方案并不像他提到的那样复杂。这是包装上下文的方式:

print(m)

   subject subject_type  value cond value_type
0        1         mild      1    A         gd
2        1         mild      2    A         fd
4        1         mild      3    B         gd
6        1         mild      4    B         fd
1        2     moderate     11    A         gd
3        2     moderate     12    A         fd
5        2     moderate     13    B         gd
7        2     moderate     14    B         fd