Hey Mark,
I'm posting the log here...
DEBUG/ActivityThread(1228): Performing launch of ActivityRecord{400bb518
[EMAIL PROTECTED]/com.android.hello.HelloAndroid}}
INFO/jdwp(1244): prepping for JDWP over ADB
INFO/jdwp(1244): PID sent as '04dc' to ADB
INFO/jdwp(1244): trying to receive file descriptor from ADB
DEBUG/AndroidRuntime(1244): --- registering native functions ---
DEBUG/dalvikvm(1228): Exception Ljava/lang/RuntimeException; from
ActivityThread.java:1758 not caught locally
DEBUG/dalvikvm(1228): Exception Ljava/lang/RuntimeException; from
ZygoteInit.java:1553 not caught locally
DEBUG/AndroidRuntime(1228): Shutting down VM
WARN/dalvikvm(1228): threadid=3: thread exiting with uncaught exception
(group=0x4000fdf8)
ERROR/AndroidRuntime(1228): Uncaught handler: thread Main exiting due to
uncaught exception
ERROR/AndroidRuntime(1228): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.android.hello/com.android.hello.HelloAndroid}:
java.lang.NullPointerException
ERROR/AndroidRuntime(1228): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1758)
ERROR/AndroidRuntime(1228): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1770)
ERROR/AndroidRuntime(1228): at
android.app.ActivityThread.access$1500(ActivityThread.java:99)
ERROR/AndroidRuntime(1228): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
ERROR/AndroidRuntime(1228): at
android.os.Handler.dispatchMessage(Handler.java:80)
ERROR/AndroidRuntime(1228): at android.os.Looper.loop(Looper.java:91)
ERROR/AndroidRuntime(1228): at
android.app.ActivityThread.main(ActivityThread.java:3052)
ERROR/AndroidRuntime(1228): at
java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(1228): at
java.lang.reflect.Method.invoke(Method.java:356)
ERROR/AndroidRuntime(1228): at
android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1547)
ERROR/AndroidRuntime(1228): at
android.os.ZygoteInit.main(ZygoteInit.java:1445)
ERROR/AndroidRuntime(1228): at android.dalvik.NativeStart.main(Native
Method)
ERROR/AndroidRuntime(1228): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(1228): at
com.android.hello.HelloAndroid.onCreate(HelloAndroid.java:24)
ERROR/AndroidRuntime(1228): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:847)
ERROR/AndroidRuntime(1228): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1728)
ERROR/AndroidRuntime(1228): ... 11 more
INFO/Process(508): Sending signal. PID: 1228 SIG: 3
INFO/dalvikvm(1228): threadid=7: reacting to signal 3
INFO/dalvikvm(1228): DALVIK THREADS:
The screen1.xml file is as below:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/screen1.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button id="@+id/hellobtn"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="Hello 1"
android:textStyle="bold"
/>
</LinearLayout>
The screen2.xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/screen2.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button id="@+id/btn2"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="Hello 2"
android:textStyle="bold"
/>
</LinearLayout>
After you said, I realized the error is in the findViewbyId function cause I
tried to comment the rest of the code and then try and set a button
attribute. And it gave the null pointer exception again.
Pls help!
On Sat, Jun 21, 2008 at 6:56 AM, Mark Murphy <[EMAIL PROTECTED]>
wrote:
>
> redhatab wrote:
> > Hi,
> >
> > Am a total beginner and I was trying out to extrapolate on the Hello
> > Android code, but I've sort of hit a road block with a null pointer
> > exception. The code is as below:
> >
> > public class HelloAndroid extends Activity
> > {
> > @Override
> > public void onCreate(Bundle icicle)
> > {
> > super.onCreate(icicle);
> >
> > setContentView(R.layout.screen1);
> >
> > Button btn = (Button) findViewById(R.id.hellobtn);
> > btn.setOnClickListener(new View.OnClickListener() {
> > public void onClick(View view) {
> > setContentView(R.layout.screen2);
> > }
> > });
> > }
> > }
> >
> > As soon as I run this, the emulator gives me a null pointer exception.
> >
> > The res/layout/screen1.xml and res/layout/screen2.xml are working
> > perfectly fine because when I just run
> > setContentView(R.layout.screen1) without the onClickListener, both the
> > screens work fine.
> >
> > I suppose the null pointer exception pops in the onClick function.
>
> If you get the exception as soon as you run the program in the emulator,
> it can't be *inside* onClick(), as you would not have clicked anything yet.
>
> More likely, btn is not getting set, because findViewById() is returning
> null, which would occur if R.id.hellobtn isn't in R.layout.screen1. The
> fact that your code compiles means that R.id.hellobtn probably exists
> somewhere...maybe in screen2?
>
> Use adb logcat (or the Eclipse equivalent) to get a stacktrace to see
> where you're failing.
>
> If you can't find the problem, post the XML layouts associated with this
> activity, and the stacktrace, and that may give us more clues.
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com
> Warescription: All titles, revisions, & ebook formats, just $35/year
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---