Bonjour à tous!
Alors voilà je suis actuellement en train de réaliser une application sur android mais j'éprouve quelques difficultés. Mon problème est que quand j'essaye de récupérer un composant à l'intérieur d'un layout en xml et que j'y applique une de ses méthode, j'obtiens tout simplement une exception.
Avec le code ci-dessous:
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 public class WiFileManagerActivity extends Activity{ public static final int DIALOG_STARTED = 0; public static final int DIALOG_EXITED = 1; public static final int DIALOG_HOST_NOT_FOUND= 2; private ServerInstance serverInstance; private static LookingForHostView lfhv; private CustomHostLoader cusDial = null; private Void v; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); showDialog(DIALOG_STARTED); lfhv = new LookingForHostView(this.getApplicationContext(), this); setContentView(lfhv); serverInstance = new ServerInstance(this); serverInstance.execute(v); } @Override protected Dialog onCreateDialog(int id) { switch(id) { case DIALOG_STARTED: cusDial = new CustomHostLoader(this); break; case DIALOG_EXITED: break; case DIALOG_HOST_NOT_FOUND: break; } return cusDial; } public void closeCustomHostLoaderDialog(){ if(cusDial != null){ cusDial.cancel(); } } }host_loader.xml
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 public class CustomHostLoader extends Dialog implements OnClickListener{ private TextView hostTv; private ImageButton helpButton; public CustomHostLoader(Activity parentActivity) { super(parentActivity); setContentView(R.layout.host_loader); hostTv = (TextView) parentActivity.findViewById(R.id.hostTv); //Ici l'exception est déclenchée hostTv.getText(); } public TextView getHostTv(){ return hostTv; } @Override public void onClick(View arg0) { Log.i("ProgLog", "Yahoo"); } }
J'obtiens:
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 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/> <TextView android:id="@+id/hostTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/progressBar1" android:layout_toRightOf="@+id/progressBar1" android:layout_marginTop="18dp" android:layout_marginLeft="10dp" android:text="Looking for host. Please wait..." /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/progressBar1" android:layout_alignRight="@+id/hostTv" android:layout_below="@+id/progressBar1" android:layout_marginTop="18dp" android:inputType="textPersonName" > </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:text="Use this IP as host IP" /> <ImageButton android:id="@+id/helpButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_alignTop="@+id/hostTv" android:layout_toRightOf="@+id/hostTv" android:src="@drawable/help" /> </RelativeLayout>
Quelqu'un saurait comment résoudre ce problème? Ce que je cherche à faire est simple en fait, c'est de pouvoir récupérer des composants de mon layout host_loader.xml dans le code java de ma classe CustomHostLoader et ainsi les modifier à ma guise, comme faire un hostTv.setText("Hello world!") par exemple. Merci d'avance
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 12-11 01:19:06.720: E/AndroidRuntime(23834): FATAL EXCEPTION: main 12-11 01:19:06.720: E/AndroidRuntime(23834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wifilemanager.client/com.wifilemanager.client.activity.WiFileManagerActivity}: java.lang.NullPointerException 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.ActivityThread.access$500(ActivityThread.java:122) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.os.Handler.dispatchMessage(Handler.java:99) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.os.Looper.loop(Looper.java:132) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.ActivityThread.main(ActivityThread.java:4123) 12-11 01:19:06.720: E/AndroidRuntime(23834): at java.lang.reflect.Method.invokeNative(Native Method) 12-11 01:19:06.720: E/AndroidRuntime(23834): at java.lang.reflect.Method.invoke(Method.java:491) 12-11 01:19:06.720: E/AndroidRuntime(23834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 12-11 01:19:06.720: E/AndroidRuntime(23834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 12-11 01:19:06.720: E/AndroidRuntime(23834): at dalvik.system.NativeStart.main(Native Method) 12-11 01:19:06.720: E/AndroidRuntime(23834): Caused by: java.lang.NullPointerException 12-11 01:19:06.720: E/AndroidRuntime(23834): at com.wifilemanager.client.dialog.CustomHostLoader.<init>(CustomHostLoader.java:29) 12-11 01:19:06.720: E/AndroidRuntime(23834): at com.wifilemanager.client.activity.WiFileManagerActivity.onCreateDialog(WiFileManagerActivity.java:46) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.Activity.onCreateDialog(Activity.java:2758) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.Activity.createDialog(Activity.java:936) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.Activity.showDialog(Activity.java:2851) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.Activity.showDialog(Activity.java:2810) 12-11 01:19:06.720: E/AndroidRuntime(23834): at com.wifilemanager.client.activity.WiFileManagerActivity.onCreate(WiFileManagerActivity.java:33) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.Activity.performCreate(Activity.java:4397) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 12-11 01:19:06.720: E/AndroidRuntime(23834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 12-11 01:19:06.720: E/AndroidRuntime(23834): ... 11 more
Partager