Bonjour ici svp j'ai un problème lors du déploiement de mon code sur l'émulateur dans Android Studio à chaque fois que je déploie mon code sans faire de traitement, ni écouteur sur les objets graphiques mon code se déploie sans problème mais une fois que fais un traitement sur un bouton comme par exemple un setEnable() sur un bouton mon code produit l'erreur suivante
app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.njikidenisgmail.myfirstapp, PID: 3800
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.njikidenisgmail.myfirstapp/com.njikidenisgmail.myfirstapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object reference
at com.njikidenisgmail.myfirstapp.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
depuis là ma formation est bloquée à cause de ça. J'aimerais si possible que vous m'aidiez à résoudre ce problème.
Voici le code mon code activity_main.xml
et mon code MainActivity.java
Code XML : 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 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android: paddingLeft="@dimen/activity_horizontal_margin" android: paddingRight="@dimen/activity_horizontal_margin" android: paddingTop="@dimen/activity_vertical_margin" tools:context="com.njikidenisgmail.myfirstapp.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/welcome_msg" android:id="@+id/welcome_id" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_val" android:id="@+id/btn_id" android:layout_below="@+id/welcome_id" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/txt_id" android:layout_alignBottom="@+id/btn_id" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_toLeftOf="@+id/btn_id" android:layout_toStartOf="@+id/btn_id" android:text="veuillez entrer votre nombre" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/screen_id" android:layout_below="@+id/btn_id" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/btn_id" android:layout_alignEnd="@+id/btn_id" android:layout_above="@+id/pb_id" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pb_id" android:max="10" android:progress="5" android:indeterminate="false" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignRight="@+id/screen_id" android:layout_alignEnd="@+id/screen_id" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/screen_id2" android:layout_below="@+id/pb_id" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/pb_id" android:layout_alignEnd="@+id/pb_id" /> </RelativeLayout>
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 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView welcome; private EditText inputnb = null; // private Button val; private TextView mess; private TextView hist; private ProgressBar bar; @override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // val = (Button) findViewById(R.id.btn_id); inputnb = (EditText) findViewById(R.id.txt_id); welcome = (TextView) findViewById(R.id.welcome_id); mess = (TextView) findViewById(R.id.screen_id); hist = (TextView) findViewById(R.id.screen_id2); bar = (ProgressBar) findViewById(R.id.pb_id); // val.setEnabled(false); // val.setOnClickListener(valListener); inputnb.clearFocus(); init(); setContentView(R.layout.activity_main); } private void init() { } private View.OnClickListener valListener = new View.OnClickListener() { @override public void onClick(View v) { Log.i("DEBUG","button cliquer"); } }; }
Partager