• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Button and ImageButton method issues

MadCode

Lurker
Hello.
There is any particular way to use Button or imageButton with every Android version ? The code I used to create a Button(I also used an ImageButton) give me crash problem with the emulator of Nexus 5 and Samsung S4 while in Samsung Galaxy Ace it works! The code I used :
Code:
MyBut.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Clicked", " ! ");
        }
    });
I also used the xml way but with android:eek:nClick="" seems that Android Studio can't find the method DoThis(View v) and so it crashes. Any suggestion?

logcat :
Code:
02-21 20:01:17.085    2323-2323/testing.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: testing.test, PID: 2323
    java.lang.RuntimeException: Unable to start activity ComponentInfo{testing.test/testing.test.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at testing.test.MainActivity.onCreate(MainActivity.java:27)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
 
@MadCode

This is the easy way to do it in Android Studio which works...

XML:
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="MadCode"
            android:text="@string/mad_code"
            android:id="@+id/button1" />
Setup your string if any.

Java:
public void MadCode(View view) {
    // Your button action here
}
 
Back
Top Bottom