JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 自定義GridView無法顯示
sunyi5017





發文: 25
積分: 0
於 2014-10-16 17:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我打算在我的畫面上放一個自定義的GridView可是他卻給我錯誤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
The following classes could not be instantiated:
- my.zack.challage.DragGridView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
 
java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
    at my.zack.challage.DragGridView.getStatusHeight(DragGridView.java:267)
    at my.zack.challage.DragGridView.<init>(DragGridView.java:58)
    at my.zack.challage.DragGridView.<init>(DragGridView.java:50)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:438)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:190)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:381)
 


請問我要怎麼去修改這些錯誤以下是我自定義GridView的程式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package my.zack.challage;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
 
public class DragGridView extends GridView 
{
  private long dragResponseMS = 0;  
  private boolean isDrag = false;  
  private int mDownX;  
    private int mDownY;  
    private int moveX;  
    private int moveY;  
    private int mDragPosition;  
    private View mStartDragItemView = null;  
    private ImageView mDragImageView;  
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowLayoutParams; 
    private Bitmap mDragBitmap;  
    private int mPoint2ItemTop ; 
    private int mPoint2ItemLeft;  
    private int mOffset2Top;  
    private int mOffset2Left;  
    private int mStatusHeight;   
    private int mDownScrollBorder;  
    private int mUpScrollBorder;  
    private static final int speed = 20;  
    private OnChangeListener OnChangeListener;
    
    public DragGridView(Context context) 
    {  
        this(context, null);  
    }  
      
    public DragGridView(Context context, AttributeSet attrs) 
    {  
        this(context, attrs, 0);  
    }  
    
  public DragGridView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
  //  if (isInEditMode()) { return; }
      mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
        mStatusHeight = getStatusHeight(context);
  }
  private Handler mHandler = new Handler();  
  private Runnable mLongClickRunnable = new Runnable() 
   {
 
    @Override
    public void run() 
    {
      // TODO Auto-generated method stub
      isDrag = true;
      mStartDragItemView.setVisibility(View.INVISIBLE);
      createDragImage(mDragBitmap, mDownX, mDownY); 
    }
    
    private void createDragImage(Bitmap mDragBitmap, int mDownX, int mDownY) {
      // TODO Auto-generated method stub
          mWindowLayoutParams = new WindowManager.LayoutParams();  
          mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; 
          mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;  
          mWindowLayoutParams.x = mDownX - mPoint2ItemLeft + mOffset2Left;  
          mWindowLayoutParams.y = mDownY - mPoint2ItemTop + mOffset2Top - mStatusHeight;  
          mWindowLayoutParams.alpha = 0.55f;
          mWindowLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;    
          mWindowLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;    
          mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE    
                      | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE ;              
          mDragImageView = new ImageView(getContext());    
          mDragImageView.setImageBitmap(mDragBitmap);    
          mWindowManager.addView(mDragImageView, mWindowLayoutParams);    
    }  
     
   };
   
   public void setOnChangeListener(OnChangeListener onChangeListener)
   {
     this.OnChangeListener = onChangeListener;  
   }
   
    public void setDragResponseMS(long dragResponseMS) 
    {  
          this.dragResponseMS = dragResponseMS;  
    }  
    
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) 
  {
    // TODO Auto-generated method stub
     switch(ev.getAction())
     {  
          case MotionEvent.ACTION_DOWN:  
              mDownX = (int) ev.getX();  
              mDownY = (int) ev.getY();  
              mDragPosition = pointToPosition(mDownX, mDownY);  
              if(mDragPosition == AdapterView.INVALID_POSITION)
              {  
                  return super.dispatchTouchEvent(ev);  
              }  
              mHandler.postDelayed(mLongClickRunnable, dragResponseMS);
              mStartDragItemView = getChildAt(mDragPosition - getFirstVisiblePosition());  
              mPoint2ItemTop = mDownY - mStartDragItemView.getTop();  
              mPoint2ItemLeft = mDownX - mStartDragItemView.getLeft();  
              mOffset2Top = (int) (ev.getRawY() - mDownY);  
              mOffset2Left = (int) (ev.getRawX() - mDownX);  
              mDownScrollBorder = getHeight() /4;    
              mUpScrollBorder = getHeight() * 3/4;  
              mStartDragItemView.setDrawingCacheEnabled(true);  
              mDragBitmap = Bitmap.createBitmap(mStartDragItemView.getDrawingCache());  
              mStartDragItemView.destroyDrawingCache();                
              break;  
          case MotionEvent.ACTION_MOVE:
          {
              int moveX = (int)ev.getX();  
                int moveY = (int) ev.getY(); 
                if(!isTouchInItem(mStartDragItemView, moveX, moveY))
                {  
                    mHandler.removeCallbacks(mLongClickRunnable);  
                }  
                break;
          }
          case MotionEvent.ACTION_UP:
          {
            mHandler.removeCallbacks(mLongClickRunnable);  
            mHandler.removeCallbacks(mScrollRunnable);
            break;
          }
     }
    return super.dispatchTouchEvent(ev);
  }
 
  private boolean isTouchInItem(View dragView, int x,
      int y) 
  {
    // TODO Auto-generated method stub
        if(dragView == null)
        {  
            return false;  
        }  
        int leftOffset = dragView.getLeft();  
        int topOffset = dragView.getTop();  
        if(x < leftOffset || x > leftOffset + dragView.getWidth())
        {  
            return false;  
        }  
        if(y < topOffset || y > topOffset + dragView.getHeight())
        {  
            return false;  
        }       
        return true;  
  }
  
  @SuppressLint("ClickableViewAccessibility")
  @Override
  public boolean onTouchEvent(MotionEvent ev) 
  {
    // TODO Auto-generated method stub
        if(isDrag && mDragImageView != null)
        {  
            switch(ev.getAction()){  
            case MotionEvent.ACTION_MOVE:  
                moveX = (int) ev.getX();  
                moveY = (int) ev.getY();  
                //拖动item  
                onDragItem(moveX, moveY);  
                break;  
            case MotionEvent.ACTION_UP:  
                onStopDrag();  
                isDrag = false;  
                break;  
            }  
            return true;  
        }  
    return super.onTouchEvent(ev);
  }
  
  private void onStopDrag() 
  {
    // TODO Auto-generated method stub
        View view = getChildAt(mDragPosition - getFirstVisiblePosition());  
        if(view != null)
        {  
            view.setVisibility(View.VISIBLE);  
        }  
        removeDragImage();  
  }
 
  private void removeDragImage() 
  {
    // TODO Auto-generated method stub
        if(mDragImageView != null)
        {  
            mWindowManager.removeView(mDragImageView);  
            mDragImageView = null;  
        }  
  }
 
  private void onDragItem(int moveX2, int moveY2) 
  {
    // TODO Auto-generated method stub
     mWindowLayoutParams.x = moveX - mPoint2ItemLeft + mOffset2Left;  
       mWindowLayoutParams.y = moveY - mPoint2ItemTop + mOffset2Top - mStatusHeight;  
       mWindowManager.updateViewLayout(mDragImageView, mWindowLayoutParams); //更新镜像的位置  
       onSwapItem(moveX, moveY);  
       mHandler.post(mScrollRunnable);  
  }
  private Runnable mScrollRunnable = new Runnable()
  {
 
    @Override
    public void run() 
    {
      // TODO Auto-generated method stub
       int scrollY;  
              if(moveY > mUpScrollBorder){  
                   scrollY = speed;  
                   mHandler.postDelayed(mScrollRunnable, 25);  
              }else if(moveY < mDownScrollBorder){  
                  scrollY = -speed;  
                   mHandler.postDelayed(mScrollRunnable, 25);  
              }else{  
                  scrollY = 0;  
                  mHandler.removeCallbacks(mScrollRunnable);  
              }  
              onSwapItem(moveX, moveY);      
              smoothScrollBy(scrollY, 10);  
    }
  };
  
 
  private void onSwapItem(int moveX, int moveY) 
  {
    // TODO Auto-generated method stub
        int tempPosition = pointToPosition(moveX, moveY);  
        if(tempPosition != mDragPosition && tempPosition != AdapterView.INVALID_POSITION)
        {  
            if(OnChangeListener != null)
            {  
                OnChangeListener.OnChange(mDragPosition, tempPosition);  
            }  
            getChildAt(tempPosition - getFirstVisiblePosition()).setVisibility(View.INVISIBLE);
            getChildAt(mDragPosition - getFirstVisiblePosition()).setVisibility(View.VISIBLE);       
            mDragPosition = tempPosition;  
        }  
  }
  
  private int getStatusHeight(Context context) {
    // TODO Auto-generated method stub
    int statusHeight = 0;  
        Rect localRect = new Rect();  
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);  
        statusHeight = localRect.top;  
        if (0 == statusHeight){  
            Class<?> localClass;  
            try {  
                localClass = Class.forName("com.android.internal.R$dimen");  
                Object localObject = localClass.newInstance();  
                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());  
                statusHeight = context.getResources().getDimensionPixelSize(i5);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
        }  
        return statusHeight;  
  }
  public interface OnChangeListener 
  {
    public void OnChange(int form, int to);
  }
 
}
 


sunyi5017 edited on 2014-10-16 17:55
reply to postreply to post
作者 Re:自定義GridView無法顯示 [Re:sunyi5017]
lnmlee





發文: 290
積分: 0
於 2014-10-17 12:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你先搞懂 java.lang.ClassCastException 是怎麼發生的就知道問題點了

reply to postreply to post
我看你腦袋驚奇 充滿創意
是萬中無一的程式奇才 我看與你有緣
這本基礎Java程式語言 就送給你了
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8