Plantage application android sur findViewById ou OnClick
Bonjour,
Je souhaite faire fonctionner un programme Android qui n'affiche pas d'erreur(s) dans Eclipse mais pour lequel le clic sur les boutons ne fait rien ou fait planter l'application.
Voici mes fichiers Java :
LocalClientActivity.java :
Code:
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
| package com.example.essais1_101;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
public class LocalClientActivity extends Activity {
private LocalService mBoundService = null;
private boolean mIsBound = false;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBoundService = ((LocalService.LocalBinder)service).getService();
Toast.makeText(LocalClientActivity.this, "connected", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBoundService = null;
Toast.makeText(LocalClientActivity.this, "disconnected", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client_activity);
findViewById(R.id.connect).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
bindService(new Intent(LocalClientActivity.this, LocalService.class), mConnection, BIND_AUTO_CREATE);
mIsBound = true;
}
});
findViewById(R.id.get_count).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
int count = mBoundService.getCount();
Toast.makeText(LocalClientActivity.this, "count :"+count, Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
} |
LocalService.java :
Code:
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
| package com.example.essais1_101;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class LocalService extends Service {
private int mCount = 0;
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public LocalService getService() {
return LocalService.this;
}
}
@Override
public void onCreate() {
Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public int getCount() {
return ++mCount;
}
} |
Mon fichier de layout : client_activity.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <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"
tools:context="com.example.essais1_101.MainActivity" >
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connect" />
<Button
android:id="@+id/get_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/connect"
android:text="@string/get_count" />
</RelativeLayout> |
et mon AndroidManifest.xml :
Code:
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
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.essais1_101"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LocalClientActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> |
Voici le log LogCat :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 05-17 03:42:01.716: W/dalvikvm(1307): threadid=1: thread exiting with uncaught exception (group=0xb4a78ba8)
05-17 03:42:01.926: E/AndroidRuntime(1307): FATAL EXCEPTION: main
05-17 03:42:01.926: E/AndroidRuntime(1307): Process: com.example.essais1_101, PID: 1307
05-17 03:42:01.926: E/AndroidRuntime(1307): java.lang.NullPointerException
05-17 03:42:01.926: E/AndroidRuntime(1307): at com.example.essais1_101.LocalClientActivity$3.onClick(LocalClientActivity.java:50)
05-17 03:42:01.926: E/AndroidRuntime(1307): at android.view.View.performClick(View.java:4438)
05-17 03:42:01.926: E/AndroidRuntime(1307): at android.view.View$PerformClick.run(View.java:18422)
05-17 03:42:01.926: E/AndroidRuntime(1307): at android.os.Handler.handleCallback(Handler.java:733)
05-17 03:42:01.926: E/AndroidRuntime(1307): at android.os.Handler.dispatchMessage(Handler.java:95)
05-17 03:42:01.926: E/AndroidRuntime(1307): at android.os.Looper.loop(Looper.java:136)
05-17 03:42:01.926: E/AndroidRuntime(1307): at android.app.ActivityThread.main(ActivityThread.java:5001)
05-17 03:42:01.926: E/AndroidRuntime(1307): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 03:42:01.926: E/AndroidRuntime(1307): at java.lang.reflect.Method.invoke(Method.java:515)
05-17 03:42:01.926: E/AndroidRuntime(1307): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-17 03:42:01.926: E/AndroidRuntime(1307): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-17 03:42:01.926: E/AndroidRuntime(1307): at dalvik.system.NativeStart.main(Native Method)
05-17 03:42:07.526: I/Process(1307): Sending signal. PID: 1307 SIG: 9 |
Merci pour votre aide.