You don't need to set the onClickListener for the button when you're
specifying an onClick in the XML layout. That
android:onClick="viBrate" will cause it to call the viBrate function
in your activity. It passes the calling view as an argument so you
just need a function like this to receive it:
public void viBrate(View v) {
// do your stuff here.
}
On Jan 28, 8:11 pm, WhiteWolf128 <[email protected]> wrote:
> Hello, community. I'm a fresh, brand new developer hoping to jump
> straight into application development, specifically for a personal
> project of mine. I have no idea what I'm doing, but I'm a fast learner
> and self-motivated into researching everything I need to know. The
> problem I'm experiencing on this note is the fact that I have no clue
> about what I should be looking for, reading, or seeing in the way of
> sources on the topic. I've built a basic application with a single
> button that closes the application cleanly, and nothing else. Now I
> want that button to cause the phone to vibrate for 2000ms, to get a
> feel for calling various classes within my application.
>
> Here's what I've written so far in the .java:
>
> package com.example.helloformstuff;
>
> import android.app.Activity;
> import android.os.*;
> import android.os.Vibrator;
> import android.widget.Button;
> import android.view.View;
>
> public class HelloFormStuff extends Activity {
> /** Called when the activity is first created. */
> Vibrator vib;
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.main);
> final Button button = (Button) findViewById(R.id.button);
> button.setOnClickListener(new View.OnClickListener() {
> public void onClick(View v) {
> long milliseconds = 2000;
> vib.vibrate(milliseconds);
> }
> });
>
> }}
>
> --------------------
> Layout XML:
>
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
> android:orientation="vertical"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> >
> <TextView android:id="@+id/text"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:text="Hello, I am a TextView"
> />
> <Button android:id="@+id/button"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:text="Vibrate"
> android:onClick="viBrate"
> />
> </LinearLayout>
> --------------------
>
> The application opens, but the button causes a null exception and
> force-closes the application. Any ideas on tutorials or examples I can
> look at? Or maybe someone could tell me exactly what's wrong, why, and
> how I can fix it?
>
> - A new dev, WhiteWolf128
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en