Bonjour,
je travail sur une application sur laquelle l'utilisateur pourrait faire une signature (comme pour recevoir un colis), et valider grâce à un bouton. J'ai au début testé uniquement le code pour faire la signature (avec un bitmap, canvas etc.), il marche bien. J'ai ensuite rajouté plusieurs onglets pour mon application est dans l'un d'eux, le code de la signature plus des boutons pour effacer et valider.
Toute ma difficulté est de lier la vue créé dans le java qui permet de dessiner la signature et les boutons définis dans le XML. J'ai réussi à trouver plusieurs exemples et explications sur différents forum et fini par mettre en place une solution (avec le constructeur MyView(Context c, AttributeSet attrs), et la <view/> dans le XML )mais une erreurs persiste.
Mon problème est que quand je teste avec le logiciel de virtualisation, l'application plante au moment ou je clic sur cette onglet et me met le message : Désolé, fermeture soudaine de l'application.
Mon code java (Tab2.java) est :
Et le XML ( onglet2.xml) correspondant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package andro.test.ids_telephone; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.os.Bundle; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class Tab2 extends Activity { private class MyView extends View { private Bitmap mBitmap = null; private Canvas mCanvas; private Path mPath; private Paint mBitmapPaint; public MyView(Context c) { super(c); } public MyView(Context c, AttributeSet attrs) { super(c, attrs); } public MyView(Context c, AttributeSet attrs, int defStyle) { super(c, attrs, defStyle); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, 400, oldw, oldh); mBitmap = Bitmap.createBitmap (w, 400, Bitmap.Config.ARGB_8888); // mBitmap2 = Bitmap.createBitmap (mBitmap, 0, 0, 480, 600); mCanvas = new Canvas(mBitmap); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); } @Override protected void onDraw(Canvas canvas) { //canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); canvas.drawBitmap(mBitmap, new Rect(0,0,480,400), new Rect(0,0,480,400), mBitmapPaint); canvas.drawPath(mPath, mPaint); } private float mX, mY; private static final float TOUCH_TOLERANCE = 4; private void touch_start(float x, float y) { mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; } private void touch_move(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); mX = x; mY = y; } } private void touch_up() { mPath.lineTo(mX, mY); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // kill this so we don't double draw mPath.reset(); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touch_start(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: touch_move(x, y); invalidate(); break; case MotionEvent.ACTION_UP: touch_up(); invalidate(); break; } return true; } } private Paint mPaint; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mView = new MyView(this); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(2); setContentView(R.layout.onglet2); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayoutOnglet2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/effacer" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginBottom="5dp" android:layout_marginLeft="42dp" android:layout_marginRight="60dp" android:text="effacer" /> <Button android:id="@+id/valider" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginBottom="5dp" android:text="valider"/> <view class="andro.test.ids_telephone.MyView" android:id="@+id/signature" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
Je fini par donner les erreures du LogCat :
J’espère avoir été assez claire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 08-30 14:21:39.952: D/AndroidRuntime(853): Shutting down VM 08-30 14:21:39.952: W/dalvikvm(853): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 08-30 14:21:39.952: E/AndroidRuntime(853): Uncaught handler: thread main exiting due to uncaught exception 08-30 14:21:39.972: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to start activity ComponentInfo{andro.test.ids_telephone/andro.test.ids_telephone.Tab2}: android.view.InflateException: Binary XML file line #26: Error inflating class andro.test.ids_telephone.MyView 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.widget.TabHost.setCurrentTab(TabHost.java:320) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.View.performClick(View.java:2364) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.View.onTouchEvent(View.java:4179) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.View.dispatchTouchEvent(View.java:3709) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 08-30 14:21:39.972: E/AndroidRuntime(853): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 08-30 14:21:39.972: E/AndroidRuntime(853): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 08-30 14:21:39.972: E/AndroidRuntime(853): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.os.Handler.dispatchMessage(Handler.java:99) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.os.Looper.loop(Looper.java:123) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.ActivityThread.main(ActivityThread.java:4363) 08-30 14:21:39.972: E/AndroidRuntime(853): at java.lang.reflect.Method.invokeNative(Native Method) 08-30 14:21:39.972: E/AndroidRuntime(853): at java.lang.reflect.Method.invoke(Method.java:521) 08-30 14:21:39.972: E/AndroidRuntime(853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 08-30 14:21:39.972: E/AndroidRuntime(853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 08-30 14:21:39.972: E/AndroidRuntime(853): at dalvik.system.NativeStart.main(Native Method) 08-30 14:21:39.972: E/AndroidRuntime(853): Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class andro.test.ids_telephone.MyView 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 08-30 14:21:39.972: E/AndroidRuntime(853): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.Activity.setContentView(Activity.java:1622) 08-30 14:21:39.972: E/AndroidRuntime(853): at andro.test.ids_telephone.Tab2.onCreate(Tab2.java:129) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 08-30 14:21:39.972: E/AndroidRuntime(853): ... 30 more 08-30 14:21:39.972: E/AndroidRuntime(853): Caused by: java.lang.ClassNotFoundException:andro.test.ids_telephone.MyView in loader dalvik.system.PathClassLoader@44e36a18 08-30 14:21:39.972: E/AndroidRuntime(853): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 08-30 14:21:39.972: E/AndroidRuntime(853): at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 08-30 14:21:39.972: E/AndroidRuntime(853): at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.createView(LayoutInflater.java:466) 08-30 14:21:39.972: E/AndroidRuntime(853): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565) 08-30 14:21:39.972: E/AndroidRuntime(853): ... 39 more
Partager