1. Your vib object is null, because you never set it to anything. You
probably want to use getSystemService() to get your hands on the, um,
Vibrator.

2. Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine
LogCat and look at  the stack trace associated with any crash.

3. Don't forget that you will need the VIBRATE permission to, er, play
with the, uh, Vibrator.

4. Don't forget that you actually need a device with a vibration motor
to see if this works (e.g., the emulator won't shake your development
machine's LCD)


On Fri, Jan 28, 2011 at 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

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://bit.ly/smand1 and http://bit.ly/smand2

-- 
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

Reply via email to