Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Application force closing as soon as it opens

Tags:

android

I'm tinkering around with android on Netbeans and did a simple math application. Somewhere along the way the application suddenly force closes as soon as it opens(On the emulator)... I tried commenting all the code in the class that hosts the main activity but to no avail. Since it doesn't give me a specific error message, I feel kind of lost.

Any reason why this is happening?

Here is the layout file as requested:

<?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/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""/>
        <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/number" />
        <EditText android:id="@+id/n"
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                        android:numeric="decimal"/>
                <Button android:id="@+id/calc"
                  android:text="@string/calculate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

As suggested I checked logcat and this is the list of errors

E/AndroidRuntime(  881): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  881): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.me.mathdroid/org.me.mathdroid.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(  881):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
E/AndroidRuntime(  881):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime(  881):        at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime(  881):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime(  881):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  881):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  881):        at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime(  881):        at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime(  881):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  881):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(  881):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime(  881):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  881): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  881):        at android.app.Activity.findViewById(Activity.java:1610)
E/AndroidRuntime(  881):        at org.me.mathdroid.MainActivity.<init>(MainActivity.java:22)
E/AndroidRuntime(  881):        at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  881):        at java.lang.Class.newInstance(Class.java:1472)
E/AndroidRuntime(  881):        at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
E/AndroidRuntime(  881):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
E/AndroidRuntime(  881):        ... 11 more
like image 744
Drahcir Avatar asked Oct 25 '09 17:10

Drahcir


2 Answers

Is this on a phone, or in the emulator? First place I'd look is in the logs. In Eclipse go to Window > Show View > Other... > Android > LogCat.

You can add your own debug logging messages in your code by

Log.d("Debug", "Informative Log Message.");

It would also be helpful if you could post your code here. Post the minimum working code example that exhibits the buggy behavior.

like image 153
Bill the Lizard Avatar answered Sep 22 '22 18:09

Bill the Lizard


I have figured out what the problem was, in the main activity I was setting an Edittext and TextView objects with the ones in the R class, before setting the contentView. Sorry for the trouble.

like image 45
Drahcir Avatar answered Sep 22 '22 18:09

Drahcir